在实体框架中索引DbGeography类型会提高查询性能吗?

本文关键字:查询 性能 高查询 框架 实体 索引 DbGeography 类型 | 更新日期: 2023-09-27 18:04:18

我的问题与这个SO线程相同,但我想知道是否对'DbGeography LocationPoints'有索引真的有必要吗?我认为简单的答案当然是肯定的,但也许空间类型有一些我不知道的特殊的东西来保持它快速搜索…

如果我要查询大量的空间数据,没有索引会影响性能吗?我还可以做些什么来提高查询性能,无论是在实体框架内部还是外部?是否有可能首先在EF代码之外的DbGeography类型上创建索引?如果有必要,我会先删除代码,然后回到ADO.net。我刚刚读到MongoDb有一个空间索引,可用于快速搜索位置数据。我应该在Azure上移动到MongoDb吗?

我从MSDN空间索引和创建空间索引中看到了一些链接,但不知道它们是否适用于我。

以防万一我需要为这个线程显示代码。下面是我的查询和用于搜索空间数据的对象。我只在本地主机上测试过,但运行几百个查询似乎有点慢。

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
}

在实体框架中索引DbGeography类型会提高查询性能吗?

我的建议是让LINQ执行SQL查询,然后在SQL管理工作室中使用它来分析它并获得执行计划,它会给你如何改进它的建议。

如果这些建议不起作用,那么去MongoDB可能会有所帮助。需要考虑的是,实体框架可能是问题所在,因为它可能在内存中加载了如此多的对象。