1

LINQ-SQL クエリ ロジックを最適化するためのパフォーマンス向上タスクに取り組んでいます。誰かが最適化されたクエリを提案できますか? これが私のクエリです

var localResponse = (from p in context.Places                                 
                     where (p.PlaceName.Contains(searchText))
                         && (p.MkTypeId == MkType.Premise)
                         && p.PlaceName != null
                         && p.ObjectState != ObjectState.Deleted
                     select new Prediction {
                                    value = p.PlaceName,
                                    id = p.APIPlaceId,
                                    reference = p.APIPlaceReference,
                                    latitude = p.Location.Latitude,
                                    longitude = p.Location.Longitude,
                                    addressId = p.AddressId,
                                    bedroom = p.Bedroom,
                                    placeTypeId = p.PlaceTypeId,
                                    placeId = p.Id
                             })
                     .Union(from p in context.Places
                            join cp in context.Places on p.Id equals cp.ParentPlaceId
                            where (p.PlaceName.Contains(searchText) || cp.PlaceName.Contains(searchText))
                                && (p.MkTypeId == MkType.Premise || p.MkTypeId == MkType.Room)
                                && p.PlaceName != null
                                && cp.PlaceName != null
                                && p.ObjectState != ObjectState.Deleted
                                && cp.ObjectState != ObjectState.Deleted
                            select new Prediction {
                                           value = cp.PlaceName + ", " + p.PlaceName,
                                           id = p.APIPlaceId,
                                           reference = p.APIPlaceReference,
                                           latitude = p.Location.Latitude,
                                           longitude = p.Location.Longitude,
                                           addressId = p.AddressId,
                                           bedroom = p.Bedroom,
                                           placeTypeId = p.PlaceTypeId,
                                           placeId = p.Id  });

予測クラスはこちら

 public class Prediction
{
    public string id { get; set; }
    public string reference { get; set; }
    public string value { get; set; }
    public double? latitude { get; set; }
    public double? longitude { get; set; }
    public int? addressId { get; set; }
    public int? bedroom { get; set; }
    public PlaceType placeTypeId { get; set; }
    public int? placeId { get; set; }
}

前もって感謝します

4

1 に答える 1

1

Places と ParentPlaces で左外部結合を実行することにより、UNION ステートメントを回避しようとします。これに加えて、クエリの主な問題は、「Contains(SearchText)」という式です。残りのすべてのレコード (削除されておらず、PlaceName が空ではない) を強制的に順次スキャン (すべてのレコードをスキャン) で 1 つずつ繰り返します。これは全文検索の対象です。そのため、データベースがそのような機能をサポートしているかどうかを確認してください。

于 2013-07-11T06:43:31.110 に答える