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 语句。除此之外,您查询的主要问题是表达式“包含(搜索文本)”。它强制所有剩余的记录(未删除且 PlaceName 不为空)在顺序扫描(扫描所有记录)中逐一迭代。这需要进行全文搜索。所以请尝试找出您的数据库是否支持这样的功能。

于 2013-07-11T06:43:31.110 回答