如何优化下面的SQL查询

时间:2019-05-07 09:33:51

标签: sql sql-query-store

我有一个后端限制,即十进制类型(aban)的Resultant列中的公式不能超过1000个字符。

如何优化此查询?

我尝试使用tempTable,但结果与预期不符。

aban = 
CASE 
WHEN ( 
    sum(isnull(a.callshandled,0)) + sum(isnull(a.totalcallsaband,0)) + sum(isnull(a.incompletecalls,0)) + sum(isnull(a.returnbusy,0)) + sum(isnull(a.returnring,0)) + sum(isnull(a.icrdefaultrouted,0)) + sum(isnull(a.ndr,0)) + sum(isnull(a.overflowout,0)) + sum(isnull(a.callsrona,0)) + sum(isnull(a.returnrelease,0)) + sum(isnull(a.callsroutednonagent,0)) + sum(isnull(a.shortcalls,0)) + sum(isnull(a.agenterrorcount,0)) + sum(isnull(a.errorcount,0))
  ) 
  = 0 THEN 
  0 
  ELSE (sum(isnull(a.totalcallsaband,0)) * 1.0) / (sum(isnull(a.callshandled,0)) + sum(isnull(a.totalcallsaband,0)) + sum(isnull(a.incompletecalls,0)) + sum(isnull(a.returnbusy,0)) + sum(isnull(a.returnring,0)) + sum(isnull(a.icrdefaultrouted,0)) + sum(isnull(a.networkdefaultrouted,0)) + sum(isnull(a.overflowout,0)) + sum(isnull(a.callsrona,0)) + sum(isnull(a.returnrelease,0)) + sum(isnull(a.callsroutednonagent,0)) + sum(isnull(a.shortcalls,0)) + sum(isnull(a.agenterrorcount,0)) + sum(isnull(a.errorcount,0)))
END 
,

以查询中公式中的字符数少于1000个且查询前后优化后的结果列具有相同值的方式优化查询。

1 个答案:

答案 0 :(得分:0)

请尝试这种方式:

DECLARE @dividend int  = 10;
DECLARE @divisor int  = 0;

SELECT COALESCE(@dividend / NULLIF(@divisor,0), 0);

因此您可以避免重复计算除数。