【发布时间】:2009-03-26 16:33:33
【问题描述】:
下面的executeTime是第一次30秒,下次我执行同一组代码时是25秒。在 SQL Profiler 中观看时,我立即看到一个登录名,然后它就在那里停留了大约 30 秒。然后,只要运行 select 语句,应用程序就会完成 ToList 命令。当我从 Management Studio 运行生成的查询时,数据库查询只需要大约 400 毫秒。它返回 14 行和 350 列。看起来将数据库结果转换为实体所需的时间非常短,以至于不会引起注意。
那么在调用数据库之前的 30 秒内发生了什么?
如果实体框架这么慢,我们就无法使用它。有什么我做错了,或者我可以改变什么来显着加快速度吗?
更新: 好吧,如果我使用 Compiled 查询,第一次需要 30 秒,第二次需要 1/4 秒。有什么办法可以加快第一次通话的速度吗?
using (EntitiesContext context = new EntitiesContext())
{
Stopwatch sw = new Stopwatch();
sw.Start();
var groupQuery = (from g in context.Groups.Include("DealContract")
.Include("DealContract.Contracts")
.Include("DealContract.Contracts.AdvertiserAccountType1")
.Include("DealContract.Contracts.ContractItemDetails")
.Include("DealContract.Contracts.Brands")
.Include("DealContract.Contracts.Agencies")
.Include("DealContract.Contracts.AdvertiserAccountType2")
.Include("DealContract.Contracts.ContractProductLinks.Products")
.Include("DealContract.Contracts.ContractPersonnelLinks")
.Include("DealContract.Contracts.ContractSpotOrderTypes")
.Include("DealContract.Contracts.Advertisers")
where g.GroupKey == 6
select g).OfType<Deal>();
sw.Stop();
var queryTime = sw.Elapsed;
sw.Reset();
sw.Start();
var groups = groupQuery.ToList();
sw.Stop();
var executeTime = sw.Elapsed;
}
【问题讨论】:
标签: c# .net entity-framework linq-to-entities