4.3.1 EF和EF 5.0 DbSet.Local比一个实际的数据库查询速度慢 [英] EF 4.3.1 and EF 5.0 DbSet.Local is slower than an actual Database query

查看:458
本文介绍了4.3.1 EF和EF 5.0 DbSet.Local比一个实际的数据库查询速度慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了约16500个城市的表,和EF数据模型(数据库优先),该数据库的数据库。我preLOAD到内存与code:

I've got a database with a table of about 16,500 cities, and an EF Data Model (Database-First) for that database. I preload them into memory with the code:

Db.Cities.Load()

......那么当它的时间使用它们,我试过下面的每个查询的:

...then when it's time to use them, I've tried each of the following queries:

Dim cities() As String = Db.Cities.Select(Function(c) c.CityName).ToArray

Dim cities() As String = Db.Cities.Local.Select(Function(c) c.CityName).ToArray

第一个查询是快速(〜10ms)的,但第二个需要大约2.3秒,以运行在第一时间(虽然它比当它被称为后,所述第一查询更快)。

The first query is fast (~10ms), but the second one takes about 2.3 seconds to run the first time (although it's faster than the first query when it's called after that).

这是没有意义的,因为SQL Server事件探查验证第一个查询击中另一台机器上的数据库,但第二个不是!

This doesn't make sense because SQL Server Profiler verifies that the first query is hitting the database on another machine, but the second isn't!

我试着关闭 Db.Configuration.AutoDetectChangesEnabled ,我尝试了pre-产生的意见。

I've tried turning off Db.Configuration.AutoDetectChangesEnabled, and I've tried pre-generating the views.

我能做些什么,使。当地更快? (不运行这个应用程序的所有客户端都将是一个快速LAN)

What can I do to make .Local faster? (Not all clients running this application are going to be on a fast LAN.)

推荐答案

你为什么不只需保存从第一个查询并使用它的字符串列表。

Why don't you simply save List of string from first query and use that instead.

List<string> cities = db.Cities.Select( x=>x.CityName).ToList();

地方可能是因为选择,这可能会做一些一致性检查的要慢。

Local may be slower because of Select, which might be doing some consistency checks.

这篇关于4.3.1 EF和EF 5.0 DbSet.Local比一个实际的数据库查询速度慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆