I have a query that runs quickly without the is null
parameter in the Where clause, but when I add it back in it takes more than 10 minutes to run.
SELECT
Parent_FRID, Name, lvl2desc, capyr, crpprjamt, empprjamt,
othprjamt AS Other_Projected, Fcrprepamt, Femprepamt, Fothrepamt, NFcrprepamt,
NFemprepamt, NFothrepamt, crpamt, empamt, othamt, Audited, Previous_Corp_Amt,
Previous_Emp_Amt, Previous_Total_Projected, Previous_Total_Reported,
Previous_Audited, crpfnl, empfnl, othfnl
FROM
Camp_Sum_6_Current_14
WHERE
cnttyp IS NULL AND
lvl2 <> '1020' AND
doncls in ('AG','CO')
I have tried everything and I can't get it figured out. I tried a subquery. I tried changing the Null values to 'None' in the original table. The same query runs fine with is not null.
4条答案
按热度按时间vyu0f0g11#
If you put an index on
doncls
, this will allow SQL-Server to filter the"AG"
and"AC"
very quickly. Then the other conditions will run on a very reduced row set. (Assuming that only a small percentage of the rows have "AG" or "AC".)An index on
lvl2
will not help much, because the condition on this column is formulated as an exclusion.Depending on how many NULLs are in
cnttyp
an index can help there too.Indexes only help, if the conditions using them are very selective. I.e. if and index helps to exclude 95% of the rows, it is useful. If it excludes only 50% of the rows, a table scan or index scan will probably be faster.
xeufq47z2#
try use cte
rsaldnfx3#
try subquery
ebdffaop4#