When I try to run the following query (or even try to get the estimated execution plan), it is very slow, 10+ minutes (I stopped waiting):
SELECT TOP 1 1
FROM File WITH (NOLOCK)
WHERE FileID = '5021ECD3-2012-62E0-1F12-000041CCB35A'
AND FileSize > 0
AND Content IS NULL
However, the following is instant:
DECLARE @test varbinary(1)
SELECT TOP 1 @test = Content
FROM File WITH (NOLOCK)
WHERE FileID = '5021ECD3-2012-62E0-1F12-000041CCB35A'
AND FileSize > 0
IF @test IS NULL
PRINT 'a'
There is a unique index on FileID. Content is a varbinary(max). Why is the optimizer not using it first? That would be pretty logicial, no? It seems it is checking all rows in the table File if the content is null or not, and then applying the WHERE FileID = '....'