简体   繁体   English

实体框架和SQL Server Profiler

[英]Entity Framework and SQL Server Profiler

I have some performance measuring issue between the EF query run through the web application and running the Profiler generated T-SQL directly into the SQL Query window . 我在通过Web应用程序运行的EF查询和将Profiler生成的T-SQL直接运行到SQL查询窗口之间有一些性能测量问题。

Following is my EF query that executes through the web application: 以下是我通过Web应用程序执行的EF查询:

IEnumerable<application> _entityList = context.applications
                    .Include(context.indb_generalInfo.EntitySet.Name)
                    .Include(context.setup_budget.EntitySet.Name)
                    .Include(context.setup_committee.EntitySet.Name)
                    .Include(context.setup_fund.EntitySet.Name)
                    .Include(context.setup_appStatus.EntitySet.Name)
                    .Include(context.appSancAdvices.EntitySet.Name)
                    .Where(e => e.indb_generalInfo != null);

                if (isIFL != null)
                    _entityList = _entityList.Where(e => e.app_isIFL == isIFL);

                int _entityCount = _entityList.Count(); // hits the database server at this line

While tracing the above EF Query in SQL Profiler it reveals that it took around 221'095 ms to execute. 在SQL事件探查器中跟踪上述EF查询时,它显示执行时需要大约221'095毫秒 (The applications table having 30,000+, indb_generalInfo having 11,000+ and appSancAdvices having 30,000+ records). (应用程序表有30,000+,indb_generalInfo有11,000+,appSancAdvices有30,000多条记录)。

However, when I copy the T-SQL from Profiler and run it directly from Query window it takes around 4'000 ms only. 但是,当我从Profiler复制T-SQL并直接从Query窗口运行时,它只需要大约4'000 ms

Why is it so? 为什么会这样?

The venom in this query is in the first words: IEnumerable<application> . 此查询中的毒液首先是: IEnumerable<application> If you replace that by var (ie IQueryable ) the query will be translated into SQL up to and including the last Count() . 如果用var替换它(即IQueryable ),查询将被翻译成SQL,包括最后一个Count() This will take considerably less time, because the amount of transported data is reduced to almost nothing. 这将花费相当少的时间,因为传输的数据量几乎没有减少。

Further, as bobek already mentioned, you don't need the Include s as you're only counting context.applications items. 此外,正如bobek已经提到的那样,您不需要Include s,因为您只计算context.applications项目。

Apart from that, you will always notice overhead of using an ORM like Entity Framework. 除此之外,您将始终注意到使用像Entity Framework这样的ORM的开销。

That's because EF needs to translate your code into TSQL first which is costly as well. 这是因为EF需要首先将您的代码转换为TSQL,这也是代价高昂的。 Look at this link here: http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/ It'll let you compile your LINQ and should help you with the speed. 在这里查看此链接: http//peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/它将让您编译LINQ并应该帮助您提高速度。 Also, do you really need that many tables for this query? 此外,您真的需要这个查询的许多表吗? Maybe you can think of a way to filter it out and only pull out what you need? 也许你可以想出一种过滤掉它的方法,只能拿出你需要的东西?

The EF definitely has a cost in terms of performance. EF在性能方面肯定会有成本。 But it also provides the flexibility to use storedprocs for complex TSQL. 但它也提供了将storageprocs用于复杂TSQL的灵活性。 But in my opinion it should be your last resort. 但在我看来,它应该是你的最后手段。

in case you interested re performance and EF. 万一你对性能和EF感兴趣。 http://msdn.microsoft.com/en-us/data/hh949853.aspx http://msdn.microsoft.com/en-us/data/hh949853.aspx

However... 然而...

EF Query in SQL Profiler it reveals that it took around 221'095 ms to execute. SQL Profiler中的EF Query显示执行大约需要221'095 ms。

then.. 然后..

copy the T-SQL from Profiler and run it directly from Query window 从Profiler复制T-SQL并直接从Query窗口运行它

Where the SQL came from is irrelevant. SQL来自哪里是无关紧要的。 Q1 took x millisecs. Q1花了x毫秒。 Based on SQL profiler info Exact same Query Q1' takes less based on SQL profiler. 基于SQL分析器信息完全相同的查询Q1'基于SQL分析器需要更少。 Which means the source of the SQL isnt the issue, it implies environmental issues are involved. 这意味着SQL的来源不是问题,它意味着涉及环境问题。

The most obvious explanation, SQL server has buffered many data pages and can much better serve the second identical request. 最明显的解释是,SQL服务器缓存了许多数据页,可以更好地为第二个相同的请求提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM