如何改进查询性能

时间:2021-12-13 06:40:46

I have a piece of query that spinning forever. I am new in DBA stuff. I would like to know what would be the first thing to deal with such problems? I looked at the Execution Plan The table has only 1 non clustered index on ClassCode. The reason I am using so many ClassCode because in SSRS report parameter @ClassCode by default will be multiple values, which is over 200 of them.

我有一个永远旋转的查询。我是DBA课程的新手。我想知道处理这些问题的第一件事是什么?我查看了执行计划,该表在ClassCode上只有一个非聚集索引。我使用这么多类代码的原因是,在SSRS report parameter @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
                                                    ) 
                            )

如何改进查询性能

2 个解决方案

#1


3  

Use conditional aggregate to do this

使用条件聚合来完成这个任务。

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

Case statement will generate 1 for the ClassCode that is present in the list else NULL will be generated. Now the count aggregate will count of 1 for each PolicyNumber. By setting = 0 we can make sure the PolicyNumber does not have any ClassCode present in given list.

Case语句将为列表else NULL中出现的类代码生成1。现在,每个保单编号的累计计数将为1。通过设置= 0,我们可以确保在给定的列表中没有任何类代码。

Count can be replaced with

计数可以替换为

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

#2


0  

First thing to do is add indexes to ClassCode and PolicyNumber. You can add non-clustered indexes to these columns.

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

After you add indexes, make sure to run it more than once so SQL can build a execution plan for you.

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

That will definitely help your query.

这肯定有助于查询。

#1


3  

Use conditional aggregate to do this

使用条件聚合来完成这个任务。

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

Case statement will generate 1 for the ClassCode that is present in the list else NULL will be generated. Now the count aggregate will count of 1 for each PolicyNumber. By setting = 0 we can make sure the PolicyNumber does not have any ClassCode present in given list.

Case语句将为列表else NULL中出现的类代码生成1。现在,每个保单编号的累计计数将为1。通过设置= 0,我们可以确保在给定的列表中没有任何类代码。

Count can be replaced with

计数可以替换为

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

#2


0  

First thing to do is add indexes to ClassCode and PolicyNumber. You can add non-clustered indexes to these columns.

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

After you add indexes, make sure to run it more than once so SQL can build a execution plan for you.

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

That will definitely help your query.

这肯定有助于查询。