如何提高查询性能
How can I improve query performance
我有一个永远旋转的查询。
我是 DBA 方面的新手。我想知道处理此类问题的第一件事是什么?我看了执行计划
table 在 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
)
)
使用条件聚合来做到这一点
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
首先要做的是为ClassCode 和PolicyNumber 添加索引。您可以向这些列添加非聚集索引。
添加索引后,确保 运行 它不止一次,以便 SQL 可以为您制定执行计划。
这一定会对您的查询有所帮助。
我有一个永远旋转的查询。 我是 DBA 方面的新手。我想知道处理此类问题的第一件事是什么?我看了执行计划 table 在 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
)
)
使用条件聚合来做到这一点
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
首先要做的是为ClassCode 和PolicyNumber 添加索引。您可以向这些列添加非聚集索引。
添加索引后,确保 运行 它不止一次,以便 SQL 可以为您制定执行计划。
这一定会对您的查询有所帮助。