为什么第一次引用 Linq to Entities 时速度如此之慢
使用实体框架 4.0,似乎第一次对实体框架对象上下文执行操作(读取或写入)时,所需时间比第二次要长几个数量级。例如,第一次查询可能需要 10 秒(是的秒),第二次可能需要 0.1 秒。
我猜测第一次构造对象上下文时,它必须构建某种幕后数据结构?它是否解析 EDMX 文件(我认为会在编译时完成?)
using Entity Framework 4.0, it seems that the first time an operation is done (read or write) against an entity framework object context it takes orders of magnatude longer than the second time. For example a query the first time may take 10 seconds (yes seconds) and the second time .1 seconds.
I'm guessing that the first time the objectcontext is constructed it has to build some sort of behind the scene data structures? Is it parsing the EDMX file (I thought would have been done at compile time?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它正在构建在后续调用中缓存的视图。
您可以预先生成视图以避免第一次性能下降:
http://www .dotnetspark.com/kb/3706-optimizing-performance.aspx
It is building views that get cached on subsequent calls.
You can pre-generate views to avoid the first time performance hit:
http://www.dotnetspark.com/kb/3706-optimizing-performance.aspx
EF 具有将实体数据模型 (EDM) 元数据加载到内存、预编译视图和其他一次性操作的启动费用,您可以尝试使用预热查询来克服这一问题。
EF has start-up expense of loading the Entity Data Model (EDM) metadata into memory, pre-compiling views and other one-time operations, you could try using warm-up query in order to get past that.
也许您正在运行查询的数据库表存在问题。因此,第一次运行 EF 时,它会编译查询、创建执行计划等,因此当您第二次运行 DB 时,它会使用查询的缓存版本。尝试向表中添加索引,看看这是否有帮助。
Maybe you have issue with a DB table that you are running your query against of. So first time you run EF it compiles your query, creates execution plan, etc, so when you are running second time DB uses cached version of your query. Try to add indexes to your table, and see if this helps.