Question

We are using SQL Server 2008. We have table with a couple million rows. We have one column that is NULLABLE but very few rows will have this column set as NULL. Maybe 100-200 rows out of millions.

We have a query to select these rows and we have an index on that column. For example

SELECT
    tbl1.Col1,
    tbl1.Col2,
    tbl1.Col3,
    tbl1.Col4,
    tbl1.Col5,
    tbl1.Col6
    tbl2.Col1,
FROM
    tbl1
    INNER JOIN tbl2 ON (tbl1.FK = tbl2.PK)
WHERE
    tbl1.NullableColumn IS NULL

Unfortunately we need a few of tbl1 columns (I am not sure how many columns is too many for use as included columns but we have 18 columns from tbl1 that we absolutely need). When we run this query the optimizer keeps suggesting an index on tbl.NullableColumn but we already have one. It is just not being used, even with a an index hint it is still not being used.

The execution plan says that it is doing an index seek on IX_tbl1_NullableColumn and then a keylookup on the clustered index (the keylookup takes 99% of the time).

  1. I have come across some people that say you should never use a NULLABLE column as index. Is this true? Does it apply in this case?
  2. Is there a smarter way of finding the handful of NULL values in the table?
Was it helpful?

Solution

Applying SP3 to SQL Server fixed this issue. It is weird and I cannot explain it. I also cannot see anything about it in the release notes. I don't know if the server just needed a reboot or if it really was SP3 that solved the issue. I think it might be SP3 because all our other servers were running SP3 and they were not showing the same problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top