简体   繁体   中英

change linq query to give best perfomance

I am working on a performance improvement task to optimize the LINQ-SQL query logic. Can someone suggest the best optimized query . Here is my query

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  });

Here is Prediction Class

 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; }
}

Thanks in advance

I would try to avoid the UNION statement by performing an left outer join on Places and ParentPlaces. Beside this, the main issue to your query is the expression "Contains(SearchText)". It forces all remaining records (not deleted and PlaceName not empty) to be iterated one by one in a sequential scan (scanning all records). This is subject to a fulltext search. So please try to find out if your database supports such a feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM