Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 实体框架4在Nerd晚餐上的搜索定位修改非常慢_Linq_Entity Framework 4_Nerddinner - Fatal编程技术网

Linq 实体框架4在Nerd晚餐上的搜索定位修改非常慢

Linq 实体框架4在Nerd晚餐上的搜索定位修改非常慢,linq,entity-framework-4,nerddinner,Linq,Entity Framework 4,Nerddinner,我修改了nerd晚餐示例,以查找指定位置附近的位置。当从平面表中选择时,性能很好,但我想拆分这些表,这样我就有了一个通用坐标表(SDB_Geography),还加入了一个表,其中包含我称之为实体类型(HB_entity)的特定数据 我创建了一个新模型,名为HbEntityModel,它存储实体、hb和地理“子模型”。现在的问题是,执行此查询大约需要5秒钟。我想这样做会使我的性能略有下降,但5秒真是太可笑了。有没有关于如何通过current table设置来提高性能的想法,或者我必须回到一个巨大的

我修改了nerd晚餐示例,以查找指定位置附近的位置。当从平面表中选择时,性能很好,但我想拆分这些表,这样我就有了一个通用坐标表(SDB_Geography),还加入了一个表,其中包含我称之为实体类型(HB_entity)的特定数据

我创建了一个新模型,名为HbEntityModel,它存储实体、hb和地理“子模型”。现在的问题是,执行此查询大约需要5秒钟。我想这样做会使我的性能略有下降,但5秒真是太可笑了。有没有关于如何通过current table设置来提高性能的想法,或者我必须回到一个巨大的平板桌上

public IEnumerable<HbEntityModel> FindByLocation(float latitude, float longitude) 
{
    return (from entity in db.SDB_Entity.AsEnumerable()
                join nearest in NearestEntities(latitude, longitude, 2) 
                on entity.EntityId equals nearest.EntityId
                join hb in db.HB_Entity
                on entity.EntityId equals hb.EntityId
                join geo in db.SDB_Geography
                on entity.GeographyId equals geo.GeographyId
                select new HbEntityModel(entity, hb, geo)).AsEnumerable();
}
public IEnumerable FindByLocation(浮动纬度、浮动经度)
{
返回(来自db.SDB_entity.AsEnumerable()中的实体)
连接最近的最近实体(纬度、经度、2)
在entity.EntityId上等于最近的.EntityId
在db.hb_实体中加入hb
在entity.EntityId上等于hb.EntityId
在db.SDB_Geography中加入geo
在entity.GeographyId上等于geo.GeographyId
选择新的HbEntityModel(实体、hb、地理)).AsEnumerable();
}
更新

所有表格都包含大约14000条记录

SDB_实体1:0/1 SDB_地理

SDB_实体1:0/1 HB_实体

搜索结果大约有70个HBentyModels


如果从单个表中选择相同的查询,则需要0.3秒,使用IQueryable而不是IEnumerable。

我在Robban的帮助下找到了如何执行该查询的方法。请参阅

我重写了函数以使用无参数构造函数,然后可以使用IQueryable

        public IQueryable<HbEntityModel> FindByLocation(float latitude, float longitude) 
    {
        return (from entity in db.SDB_Entity
                    join nearest in NearestEntities(latitude, longitude, 2) 
                    on entity.EntityId equals nearest.EntityId
                    join hb in db.HB_Entity
                    on entity.EntityId equals hb.EntityId
                    join geo in db.SDB_Geography
                    on entity.GeographyId equals geo.GeographyId
                    select new HbEntityModel() { Shared=entity, Specific=hb, Geography=geo }).AsQueryable();
    }
public IQueryable FindByLocation(浮动纬度、浮动经度)
{
返回(来自db.SDB_实体中的实体)
连接最近的最近实体(纬度、经度、2)
在entity.EntityId上等于最近的.EntityId
在db.hb_实体中加入hb
在entity.EntityId上等于hb.EntityId
在db.SDB_Geography中加入geo
在entity.GeographyId上等于geo.GeographyId
选择new HbEntityModel(){Shared=entity,Specific=hb,Geography=geo});
}

现在执行查询大约需要0.4秒,这在某种程度上是可以接受的。希望当我的机器到达时,事情会更快。如果有人能给我提示如何改进查询、使用存储过程或设置一些索引,我将不胜感激。

为什么第一个
.AsEnumerable()
?您在对象空间中完成了所有工作。毫不奇怪,速度很慢-您刚刚将4个表下载到内存中。如果没有第一个AsEnumerable,我会得到“LINQ to Entities中只支持无参数构造函数和初始值设定项。“是的,但您只需要在联接结果上使用它,而不需要输入。您根本不需要存储过程,但我建议您对其进行分析,以查看执行的查询,并签出执行计划。谢谢您的建议。”。我将在下周末之前把事情准备好并开始运作,所以我会在晚些时候研究这个问题。这不再是一个表演的阻碍。