[英]EF Linq query is 100x slower than stored procedure
I ran a test execute a Linq query compared to a stored procedure call using both in EF DataContext
. 与使用EF
DataContext
的存储过程调用相比,我运行了一个测试执行Linq查询。 And it seems like the stored procedure call is around 100x faster - why is that? 并且看起来存储过程调用快了大约100倍 - 为什么会这样?
LINQ example: LINQ示例:
var results = (from I in db.MyTable
select I).toList();
Stored procedure: 存储过程:
....
SELECT *
FROM myTable T
INNER JOIN TABLE2
INNER JOIN TABLE3
CALL to stored procedure CALL到存储过程
var results = this.Database.SQlQuery<MyModel>("spMyStorePro").ToList()
The Linq execution would take like 10-15 seconds, while the stored procedure call would take like 1-2 sec if not less. Linq执行需要10-15秒,而存储过程调用将花费1-2秒,如果不是更少。
the model contains several entities 该模型包含几个实体
MyTableModel
public int KEY {Get;set;}
public virtual TABLE1 table1Info {get; set;}
public virtual TABLE2 table2Info {get; set;}
Is this common or am I setting up my model incorrectly? 这是常见的还是我错误地设置了我的模型?
One thing I would mention is that my MODEL has a few nested models that may cause the slow performance. 我要提到的一件事是我的MODEL有一些嵌套模型可能会导致性能下降。
UPDATED 更新
So here is something interesting I found: 所以我找到了一些有趣的东西:
I grab the sql query from sql monitor that LINQ spits out. 我从LINQ吐出的sql监视器中获取sql查询。
it looks something like this 它看起来像这样
SELECT [EXtent1].COL1, [Extent2].COL2 FROM table1 as extent1 left outer join table 2 as extent2 SELECT [EXtent1] .COL1,[Extent2] .COL2 FROM table1 as extent1 left outer join table 2 as extent2
if I execute the above query it takes 9 sec to execute each time. 如果我执行上述查询,每次执行需要9秒。 The interesting is that if I remove COL2 from the select list, it executes in 2 secs.
有趣的是,如果我从选择列表中删除COL2,它将在2秒内执行。
WHy would removing a column from the select list improve the speed? 为什么从选择列表中删除列会提高速度? This is outside of EF and LINQ now, this is a question in sql
现在这不在EF和LINQ之内,这是sql中的一个问题
Things to review 要检讨的事情
var query = context.Query<Thing>(
include: t => t.Child.Select(v => v.Grandchild))
.Where(t => t.Foo == "bar"); // includes descendants
var query = context.Query<Thing>()
.Where(t => t.Foo == "bar"); // excludes descendants
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.