实体框架 4 在书呆子晚餐 FindByLocation 修改上真的很慢
我修改了书呆子晚餐示例以查找指定位置附近的位置。当从平面表中进行选择时,性能良好,但我想拆分表,因此我有一个通用坐标表(SDB_Geography),并且还加入了一个包含我所说的实体类型(HB_Entity)的特定数据的表。
我制作了一个名为 HbEntityModel 的新模型,它存储实体、hb 和地理“子模型”。现在的问题是这个查询大约需要 5 秒才能执行。我认为这样做会稍微降低性能,但 5 秒实在是太荒谬了。关于如何提高当前桌子设置的性能有什么想法,还是我必须回到巨大的平板桌子?
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();
}
更新
所有表包含大约 14000 条记录。
SDB_Entity 1:0/1 SDB_Geography
SDB_Entity 1:0/1 HB_Entity
搜索产生大约 70 个 HbEntityModel。
如果从单个表中选择相同的查询需要 0.3 秒,使用 IQueryable 而不是 IEnumerable。
I have modified the nerd dinner example to find locations in the vicinity of specified position. When selecting from a flat table performance is good, but I wanted to split up the tables so I have a generic coordinates table (SDB_Geography) and also join in a table with specific data for what i call the entity type (HB_Entity).
I have made a new model called HbEntityModel which stores entity, hb and geography "sub models". Now the problem is that this query takes around 5 seconds to execute. I figured I would get a slight performance decrease by doing this but 5 seconds is just ridiculous. Any ideas on how to improve the performance with currrent table setup or do i have to go back to a monstrous flat 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();
}
UPDATE
All tables contains around 14000 records.
SDB_Entity 1:0/1 SDB_Geography
SDB_Entity 1:0/1 HB_Entity
The search yields around 70 HbEntityModels.
If selecting from single table the same query takes 0.3s, using IQueryable instead of IEnumerable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 Robban 的帮助下找到了如何做到这一点”。请参阅这个 我重写了
该函数以使用无参数构造函数,然后可以使用 IQueryable。
现在,查询需要大约 0.4 秒的时间来执行,希望当我的平均机器到达时,事情会更快。我提示如何改进查询、使用存储过程或设置一些索引,我将不胜感激。
I found out how to do it with some help from Robban". See this post.
I rewrote the function to use a parameterless constructor and could then use IQueryable.
The query now takes around 0.4 seconds to execute which is somewhat acceptable. Hopefully things will be faster when my mean machine arrives. If someone could give me hints on how to improve the query, use a stored procedure or setup some index, i would be more than grateful.