SQL Server with Mr. Denny

Jan 7 2015   5:00PM GMT

If I have a filtered index, what do I need to have in my query to make SQL use it?

Denny Cherry Denny Cherry Profile: Denny Cherry

Database index strategy
SQL Server
SQL Server 2008
SQL Server 2008 R2
SQL Server 2012
SQL Server 2014

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.


 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: