简体   繁体   中英

Would indexing a DbGeography type in Entity Framework improve query performance?

My question is the same as this SO thread , but I would like to know if having an index on 'DbGeography LocationPoints' is really necessary? I think the simple answer is yes of course, but maybe the spacial type has something under the hood that does something special I'm unaware of to keep it fast for searching through...

If I'm going to be querying through lots of spacial data will I have a performance hit with no index? Is there something else I can do to increase the query performance, either inside or outside of entity framework? Is it possible to create an index on a DbGeography type outside of EF code first? I will drop code first if I have to and go back to ADO.net. I just read that MongoDb has a spatial index that can be used for fast searching of location data. Should I move to MongoDb on Azure?

I see a few links here from MSDN spacial indexes and create spacial index but don't know if they would apply to me.

Just in case I need to show code for this thread here ya go. Here is my query and the objects that are used to search through the spacial data. I've only tested on local host but running through a few hundred queries seems kinds slow.

var yogaSpaces = (from u in context.YogaSpaces
                         orderby u.Address.LocationPoints.Distance(myLocation)
                         where ((u.Address.LocationPoints.Distance(myLocation) <= 8047)
                         && (u.Events.Any(e => e.DateTimeScheduled >= classDate)))
                         select u).ToPagedList(page, 10);

public class YogaSpaceAddress
{
    //omitted code here

    public DbGeography LocationPoints { get; set; }
    // omitted code here
}

public class YogaSpaceEvent
{
    public int YogaSpaceEventId { get; set; }
    //public string Title { get; set; }
    [Index]
    //research more about clustered indexes to see if it's really needed here
    //[Index(IsClustered = true, IsUnique = false)] 
    public DateTime DateTimeScheduled { get; set; }
    //omitted code here
}


public class YogaSpace
{
    //omitted code here

    public virtual YogaSpaceAddress Address { get; set; }

    //omitted code here

    public virtual ICollection<YogaSpaceEvent> Events { get; set; }

    //omitted code here
}

My suggestion would be to get the SQL query executed by LINQ and then use that in the SQL management studio to analyze it and get the execution plan and it'll give you suggestions on how to improve it.

If those suggestions don't work then it might be helpful to go to MongoDB. Something to consider is that the Entity Framework may be the problem because it may be loading so many objects in to memory.

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