什么是这个的最佳替代查询 [英] what is best alternate query for this one

查看:101
本文介绍了什么是这个的最佳替代查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完全满足我的要求的查询,但问题是,当该表中的数据将增加然后它将消耗更多时间,我想在EF中交替查询以便我不会得到那个机会。我的查询如下:

i have a query that is full filling my requirement ,but problem is that when data in that table will increase then it will consume more time ,i want to alternate query in EF so that i will not get that chance. my query is as follow:

var ranks = _service.GetAllEmployeeDuty().OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).Where(x => x.ToSector_Id == sectorId).ToList();

推荐答案

你说当该表中的数据增加时会消耗更多时间



这不一定像你想象的那么糟糕。

这真的取决于Linq提供商。



让我解释一下。

当你执行这个查询时,引擎不会按顺序依次运行它:

1 - GetAllEmployeeDuty()

2 - OrderByDescending(x => x.EndDate)

3 - GroupBy(x => x.Employee_Id)

4 - 选择(x => x.First())

5 - 其中(x => x.ToSector_Id == sectorId)

6 - ToList();



相反,linq只会构建一个表达式树,然后将这个表达式树转换为一个大的SQL请求并将其传递给SQL引擎。

SQL专业是估计哪条路径更快。

SQL优化引擎可能从最小的表开始,甚至可以根据每个表中的记录数改变其策略。 br />


总而言之,这并不一定像你想象的那么糟糕。
You say "when data in that table will increase then it will consume more time"

This is not necessarily as bad as you think.
It really depends of the Linq provider.

Let me explain.
When you execute this query, the engine is not going to run it sequentially in this order:
1 - GetAllEmployeeDuty()
2 - OrderByDescending(x => x.EndDate)
3 - GroupBy(x => x.Employee_Id)
4 - Select(x => x.First())
5 - Where(x => x.ToSector_Id == sectorId)
6 - ToList();

On the contrary linq will only build an expression tree, then transform this expression tree into a big SQL request and pass it to the SQL engine.
The SQL speciality is to estimate which path is faster.
The SQL optimization engine will probably start with the smallest tables and can even change its strategy depending on the number of records in each tables.

In summary, again, this is not necessarily as bad as you think.


我会一直建议,使用group by和最后排序
I will always suggest, use group by and order by in last


这篇关于什么是这个的最佳替代查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆