January 14, 2014 at 11:11 pm
Comments posted to this topic are about the item Filtered Index vs Indexed View
January 14, 2014 at 11:11 pm
Thanks
Hope this helps...
Ford Fairlane
Rock and Roll Detective
January 15, 2014 at 12:35 am
Nice questions...
January 15, 2014 at 12:39 am
Good one....
January 15, 2014 at 3:09 am
Could you please explain more details about the pass of query 4 and the fail of query 6?
January 15, 2014 at 3:20 am
I had the very same question, here's a post which discusses why local variables cause problems with matching to filtered indexes:
http://blogs.msdn.com/b/timchapman/archive/2012/08/27/the-drawback-of-using-filtered-indexes.aspx
January 15, 2014 at 3:50 am
jchu 39760 (1/15/2014)
Could you please explain more details about the pass of query 4 and the fail of query 6?
The error 8622 is raised when you are searching a value in a table, BUT you instruct the optimizer to use a filtered index. Maybe, that the value exists in the table, but not in the filtered index. As you can see, this is a contradiction and an error is raised. The data in the table and in the filtered index may be different.
So the “SELECT * FROM table WITH(INDEX(idx_filtered)) WHERE id=99” asks for a row where id = 99 that exists in the table, but not in the filtered index: this is a contradiction.
At last, in the “SELECT * FROM table WITH(INDEX(idx_filtered)) WHERE id=@xxx”, the optimizer does not know the value of @xxx and if it is in the table, so it raises an error.
January 15, 2014 at 4:22 am
This was removed by the editor as SPAM
January 15, 2014 at 5:09 am
January 15, 2014 at 8:55 am
One more reason for me to avoid index query hints, unless it's completely necessary and throughly tested.
January 15, 2014 at 12:02 pm
jchu 39760 (1/15/2014)
Could you please explain more details about the pass of query 4 and the fail of query 6?
Query 2 (I know, you didn't ask) fails because you search for values < 5. Theoretically, there can be a value of -3 in the table. The filtered index would not include that value, so the optimizer is unable to produce a plan that is guaranteed to return correct results. For query 1, this objection is not relevant - you ask for values returned from the view, so not returning rows that are filtered out by the view condition is a given.
Query 4 asks for a specific value, and that value IS included in the filtered index. Based on the metadata, SQL Server can guarantee that all the rows you want returned will be in the filtered index.
Query 6 asks for the same value, but using a variable. The way the optimzer works means that when the query is parsed and a plan is compiled, the run-time value of the variable is unknown. There is no way to produce a plan that is guaranteed to return correct results for every value of the variable.
I was surprised to see that the question instructs people to run the query first. Should that not have been "without running the query, ..."? (That's how I did it - much more fun that way!)
January 15, 2014 at 12:25 pm
That was an excellent question; thank you for it. Query hints...you'd better know what you're doing. Most times it's better to let the optimizer do what it does best and optimize the query. Granted, it sometimes makes bad choices, but all in all I think it works pretty well.
January 15, 2014 at 6:22 pm
Ed Wagner (1/15/2014)
That was an excellent question; thank you for it. Query hints...you'd better know what you're doing. Most times it's better to let the optimizer do what it does best and optimize the query. Granted, it sometimes makes bad choices, but all in all I think it works pretty well.
+1. I try to let the optimizer do its work whenever possible. Great question Carlo.
January 16, 2014 at 12:58 am
Thanks for the excellent question.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 15 posts - 1 through 15 (of 27 total)
You must be logged in to reply to this topic. Login to reply