Why filtered index on IS NULL value is not used?

Why filtered index on IS NULL value is not used?



Assume we have a table definition like this:


CREATE TABLE MyTab (
ID INT IDENTITY(1,1) CONSTRAINT PK_MyTab_ID PRIMARY KEY
,GroupByColumn NVARCHAR(10) NOT NULL
,WhereColumn DATETIME NULL
)



And a filtered non-clustered index like this:


CREATE NONCLUSTERED INDEX IX_MyTab_GroupByColumn ON MyTab
(GroupByColumn)
WHERE (WhereColumn IS NULL)



Why this index is not "covering" for this query:


SELECT
GroupByColumn
,COUNT(*)
FROM MyTab
WHERE WhereColumn IS NULL
GROUP BY GroupByColumn



I'm getting this execution plan:



enter image description here



The KeyLookup is for the WhereColumn IS NULL predicate.



Here is the plan: https://www.brentozar.com/pastetheplan/?id=SJcbLHxO7




2 Answers
2



Why this index is not "covering" for this query:



No good reason. That is a covering index for that query.



Please vote for the feeback item here: https://feedback.azure.com/forums/908035-sql-server/suggestions/32896348-filtered-index-not-used-when-is-null-and-key-looku



And as a workaround include the WhereColumn in the filtered index:


WhereColumn


CREATE NONCLUSTERED INDEX IX_MyTab_GroupByColumn
ON MyTab (GroupByColumn) include (WhereColumn)
WHERE (WhereColumn IS NULL)






This was reported over a decade ago by Gail Shaw. Then Connect died. Closest I can find now is feedback.azure.com/forums/908035-sql-server/suggestions/…

– Paul White
Sep 7 '18 at 18:53




I had the same issue I think when doing some testing weeks ago.
I have a query with a primary predicate that requires that results returned have a NULL closedatetime and I thought about using a filtered index as 25K of 2M+ records are NULL
and this figure will decrease very soon.



The filtered index didn't get used - I assumed due to 'non-uniqueness' or commonality - until I found a Microsoft support article that says:



To resolve this issue, include the column that is tested as NULL in the returned columns. Or, add this column as include columns in the index.



So adding the column to the Index (or Include) seems to be the official MS response.






It's a viable workaround, until when (and if) they fix it.

– ypercubeᵀᴹ
Jan 13 at 12:59



Thanks for contributing an answer to Database Administrators Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)