如何提高查询性能

时间:2016-11-18 17:50:49

标签: sql-server tsql reporting-services execution-time notin

我有一个永远旋转的问题。 我是DBA新手。我想知道处理这些问题的第一件事是什么?我查看了执行计划 该表在ClassCode上只有1个非聚集索引。我使用这么多ClassCode的原因是因为在SSRS报告中参数@ClassCode默认是多个值,超过200个。

SELECT  PolicyNumber 
FROM    tblClassCodesPlazaCommercial 
WHERE   PolicyNumber NOT IN (
                            SELECT  PolicyNumber 
                            FROM    tblClassCodesPlazaCommercial 
                            WHERE   ClassCode IN    (
                                                    33489,31439,41894,68189,01239,01199,68528,67139,68128,739889,
                                                    33428,5561,68428,01484,5281,67227,01184,50199,23528,33283,03228,
                                                    50499,41594,50427,5181,31484,03199,6481,68239,50439,68489,36127,    
                                                    50472,67128,23149,33439,03149,5452,23189,50228,01428,69183,50527,
                                                    67499,739189,50159,31183,33499,01283,33128,50239,6451,33159,
                                                    21199,67272,67127,69427,5451,23239,67199,67449,67189,01599,40228,
                                                    50184,5551,33299,7398,40179,40128,50139,7381,33199,50497,23428,33129,
                                                    738299,67149,40184,23128,69199,68499,50299,31449,40497,68169,67197,
                                                    5191,67259,5252,03489,67459,21299,5262,01181,03428,31483,68183,68228,
                                                    31199,40484,738199,03499,31499,40189,7382,67439,21527,50449,01427,
                                                    68199,5453,50528,36228,50259,68299,50227,23459,33528,40199,40427,
                                                    21289,42594,5283,34489,5251,21228,50197
                                                    ) 
                            )

enter image description here

2 个答案:

答案 0 :(得分:3)

使用条件聚合来执行此操作

SELECT  PolicyNumber 
FROM    tblClassCodesPlazaCommercial 
group by PolicyNumber
having count(case when ClassCode IN  (33489,31439,41894,..) then 1 end) = 0

Case语句将为列表中的ClassCode生成1,否则将生成NULL。现在,每个PolicyNumber的计数聚合将计为1。通过设置= 0,我们可以确保PolicyNumber在给定列表中没有任何ClassCode

Count可以替换为

SUM(case when ClassCode IN  (33489,31439,41894,..) then 1 else 0 end) = 0

答案 1 :(得分:0)

首先要做的是向ClassCode和PolicyNumber添加索引。您可以向这些列添加非聚集索引。

添加索引后,请确保多次运行索引,以便SQL可以为您构建执行计划。

这肯定有助于您的查询。