简体   繁体   English

EF SqlQuery查询比直接查询慢20倍是否正常?

[英]Is it normal for an EF SqlQuery query to be 20x slower than a direct query?

I am trying to track down performance issues. 我试图追踪性能问题。 I am running the following SQL query against a SQL Server Express database: 我正在对SQL Server Express数据库运行以下SQL查询:

SELECT COUNT(OrderID)
  FROM FutureOrderHeader
 WHERE ScheduledFulfillmentTime >= {0}
   AND ScheduledFulfillmentTime <  {1}
   AND SplitStatus <> 2
   AND Deleted = 0
   AND OrderMode = {2}

When I run the command using Entity Framework as follows: 当我使用实体框架运行命令时,如下所示:

var results = ((DbContext)this._context).Database.SqlQuery<int>(SQL,start.BoxToSqlDataTime(),end.BoxToSqlDataTime(), (int)mode).Single();

It is approximately 20x slower then if I run the command through ADO.NET. 如果我通过ADO.NET运行命令,则速度大约要慢20倍。 Using SQL Server Profiler and EF Profiler, I was able to determine that the queries are the same. 使用SQL Server Profiler和EF Profiler,我可以确定查询是相同的。 Executing this query through EF approx. 通过EF大约执行此查询。 720 times takes an average of 2837ms. 720次平均需要2837ms。 Running the same query using straight ADO.NET takes an average 146ms. 使用直接ADO.NET运行相同的查询平均需要146毫秒。

In SQL Server Profiler, the actual SQL text which is executed as well as the duration, cpu usage, reads, etc. are the same whether I am using EF or ADO.NET. 在SQL Server Profiler中,无论我使用的是EF还是ADO.NET,执行的实际SQL文本以及持续时间,cpu使用率,读取次数等都是相同的。 According to SQL Server Profiler, the query itself takes between 0ms and 2ms regardless of whether I am using EF or straight ADO.NET. 根据SQL Server Profiler,无论我使用的是EF还是直接的ADO.NET,查询本身的时间都在0ms到2ms之间。

I was wondering if this performance hit is normal or if I am missing something. 我想知道这种性能下降是否正常还是我缺少什么。

Thanks! 谢谢!

No, I would not say that is normal. 不,我不会说这是正常的。 While there are things that EF and Linq-to-SQL do poorly that do result in some horribly performing queries, getting a simple count with a where clause should perform just fine. 尽管EF和Linq-to-SQL在某些方面做得不好,但确实会导致某些可怕的查询执行,但是使用where子句进行简单计数应该可以正常工作。

I would recommend profiling the EF call in call to see what is actually being executed. 我建议在调用中对EF调用进行概要分析,以查看实际执行的操作。 Looking at EF Code query you provided, it doesn't appear to be the same query. 查看您提供的EF代码查询,它似乎不是同一查询。 You can then alter the EF code or build appropriate indices to get the performance you are looking for. 然后,您可以更改EF代码或构建适当的索引以获得所需的性能。

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

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