Filtered indexes are great, because they let you exclude rows from the index. But what do you need to include in your where clause to use the filtered index?
The answer here is surprisingly straight forward. You need to use the exact same where clause value in your queries where clause as is in the definition of the index. You also need that part of the where clause to be a text string in the query, not a parameter.
So for example “WHERE Active=1” will work, where “WHERE Active=@Active” won’t. The reason for this is that at compile time the SQL Server won’t know what the value within the @Active parameter is, so it can’t use the filtered index. It has to do this because you could pass in 0 to the @Active parameter and then the plan with the filtered index wouldn’t be valid.
Contact the Author | Contact DCAC