[英]SQL Server 2012 - Optimizing SQL query with functions in select statement
Does anyone have any suggestions on making the following select statement more efficient? 有人对提高以下选择语句的效率有什么建议吗? It is a very simple query but the SSRS using the code hits the timeout set. 这是一个非常简单的查询,但是使用代码的SSRS达到了超时设置。
SELECT
G.A
,B = IsNull(Avg(CAST(T.B as decimal)), 0)
,C = Sum(T.C)
,D = IsNull(Avg(CAST(T.D as decimal)), 0)
FROM
TableA as G
INNER JOIN
TableB as T ON T.An_ID = G.An_ID
group by G.A
This is a snippet of the code with identical grouping in the bigger script of the SP that the SSRS is using. 这是SSRS所使用的SP的较大脚本中具有相同分组的代码片段。
For this query: 对于此查询:
SELECT G.A, B = IsNull(Avg(CAST(T.B as decimal)), 0),
C = Sum(T.C), D = IsNull(Avg(CAST(T.D as decimal)), 0)
FROM TableA G INNER JOIN
TableB T
ON T.An_ID = G.An_ID
GROUP BY G.A;
You want indexes on TableB(An_ID)
and TableA(An_Id, A)
. 您想要在TableB(An_ID)
和TableA(An_Id, A)
上建立索引。
My guess is that the joins are producing a very large number of intermediate rows. 我的猜测是,联接产生了大量中间行。 You can get the count by doing: 您可以通过执行以下操作获得计数:
select sum(a.cnt * b.cnt)
from (select an_id, count(*) as cnt from tablea group by an_id) a join
(select an_id, count(*) as cnt from tableb group by an_id) b
on a.an_id = b.an_id;
You can find the offensive combinations: 您可以找到令人反感的组合:
select top 100 a.cnt * b.cnt, a.an_id
from (select an_id, count(*) as cnt from tablea group by an_id) a join
(select an_id, count(*) as cnt from tableb group by an_id) b
on a.an_id = b.an_id
order by a.cnt * b.cnt desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.