SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Indexed Views


Indexed Views

Author
Message
Toreador
Toreador
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3863 Visits: 8126
On Enterprise edition the results are the same - the view index is used.

On both this and Developer edition I get a warning for each statement
"Warning: Index hints supplied for view 'dbo.v1' will be ignored."
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19019 Visits: 12426
Toreador (2/13/2012)
On Enterprise edition the results are the same - the view index is used.

On both this and Developer edition I get a warning for each statement
"Warning: Index hints supplied for view 'dbo.v1' will be ignored."

*lol* I was so wrapped up in checking the execution plan that I completely forgot to check out the messages pane. Yes, I get this warning as well (Developer Edition).

I'm still interested in the results of running this code in non-Enterprise and non-Developer editions, but given this warning message, I estimate a 95% probability that the index hints will be ignored on those editions as well.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Revenant
Revenant
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12645 Visits: 5010
Hugo Kornelis (2/13/2012)
Toreador (2/13/2012)
On Enterprise edition the results are the same - the view index is used.

On both this and Developer edition I get a warning for each statement
"Warning: Index hints supplied for view 'dbo.v1' will be ignored."

*lol* I was so wrapped up in checking the execution plan that I completely forgot to check out the messages pane. Yes, I get this warning as well (Developer Edition).

I'm still interested in the results of running this code in non-Enterprise and non-Developer editions, but given this warning message, I estimate a 95% probability that the index hints will be ignored on those editions as well.

Hugo

I ran your code under R2 DataCenter, up to date, and got 'Warning: Index hints supplied for view 'dbo.v1' will be ignored.' 5 times.
jascunce
jascunce
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

Group: General Forum Members
Points: 815 Visits: 760
Revenant (2/13/2012)
Hugo Kornelis (2/13/2012)
Toreador (2/13/2012)
On Enterprise edition the results are the same - the view index is used.

On both this and Developer edition I get a warning for each statement
"Warning: Index hints supplied for view 'dbo.v1' will be ignored."

*lol* I was so wrapped up in checking the execution plan that I completely forgot to check out the messages pane. Yes, I get this warning as well (Developer Edition).

I'm still interested in the results of running this code in non-Enterprise and non-Developer editions, but given this warning message, I estimate a 95% probability that the index hints will be ignored on those editions as well.

Hugo

I ran your code under R2 DataCenter, up to date, and got 'Warning: Index hints supplied for view 'dbo.v1' will be ignored.' 5 times.


The same results regarding the warning message were returned from SQL 2008 R2 instances, Std edition, RTM as well as sp1.
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19019 Visits: 12426
jascunce (2/13/2012)
Revenant (2/13/2012)
I ran your code under R2 DataCenter, up to date, and got 'Warning: Index hints supplied for view 'dbo.v1' will be ignored.' 5 times.

The same results regarding the warning message were returned from SQL 2008 R2 instances, Std edition, RTM as well as sp1.

Thanks, Revenant and Jascunce! That confirms my suspicion.

Did you also check the actual execution plan to confirm if the query optimizer used (an index on) the base table or (an index on) the view?


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
jascunce
jascunce
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

Group: General Forum Members
Points: 815 Visits: 760
Hugo Kornelis (2/13/2012)
jascunce (2/13/2012)
Revenant (2/13/2012)
I ran your code under R2 DataCenter, up to date, and got 'Warning: Index hints supplied for view 'dbo.v1' will be ignored.' 5 times.

The same results regarding the warning message were returned from SQL 2008 R2 instances, Std edition, RTM as well as sp1.

Thanks, Revenant and Jascunce! That confirms my suspicion.

Did you also check the actual execution plan to confirm if the query optimizer used (an index on) the base table or (an index on) the view?


This is interesting. The actual execution plan from the Std instance with sp1 reports that the clustered index from the base table, not the view, was used in each of the 5 queries. Sorry I couldn't get that info from the RTM instance since it was just upgraded but I would think the results would still be the same :-).

I compared that with the execution of the code in a SQL 2008 R2 Enterprise instance with sp1 and the results were like you had reported from the Developer edition; the clustered index from the view was used.
Revenant
Revenant
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12645 Visits: 5010
jascunce (2/13/2012)
Hugo Kornelis (2/13/2012)
jascunce (2/13/2012)
Revenant (2/13/2012)
I ran your code under R2 DataCenter, up to date, and got 'Warning: Index hints supplied for view 'dbo.v1' will be ignored.' 5 times.

The same results regarding the warning message were returned from SQL 2008 R2 instances, Std edition, RTM as well as sp1.

Thanks, Revenant and Jascunce! That confirms my suspicion.

Did you also check the actual execution plan to confirm if the query optimizer used (an index on) the base table or (an index on) the view?


This is interesting. The actual execution plan from the Std instance with sp1 reports that the clustered index from the base table, not the view, was used in each of the 5 queries. Sorry I couldn't get that info from the RTM instance since it was just upgraded but I would think the results would still be the same :-).

I compared that with the execution of the code in a SQL 2008 R2 Enterprise instance with sp1 and the results were like you had reported from the Developer edition; the clustered index from the view was used.

The same for the DataCenter Edition.

The plan is attached.
Attachments
Hugo.sqlplan (8 views, 43.00 KB)
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19019 Visits: 12426
Revenant (2/13/2012)
jascunce (2/13/2012)
This is interesting. The actual execution plan from the Std instance with sp1 reports that the clustered index from the base table, not the view, was used in each of the 5 queries. Sorry I couldn't get that info from the RTM instance since it was just upgraded but I would think the results would still be the same :-).

I compared that with the execution of the code in a SQL 2008 R2 Enterprise instance with sp1 and the results were like you had reported from the Developer edition; the clustered index from the view was used.

The same for the DataCenter Edition.

The plan is attached.


All exactly as I expected. This confirms that on editions below Enterprise, the only way to get the optimizer to use an index on the view is to use the NOEXPAND hint; using an index hint will not work, as they are ignored on views.

Remember, on Enterprise Edition (and hence also on the feature-equal Developer Edition and on DataCenter, which is essentially Enterprise++), the optimizer will automatically consider using indexes on views, regardless of whether the query references the view or the base tables. On lower editions (such as Standard, Workgroup, and Express), the optimizer will never consider using indexes on views, unless the NOEXPAND hint is used to force the use of an index on the view.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
jascunce
jascunce
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

Group: General Forum Members
Points: 815 Visits: 760
Hugo Kornelis (2/13/2012)
[quote]Revenant (2/13/2012)

All exactly as I expected. This confirms that on editions below Enterprise, the only way to get the optimizer to use an index on the view is to use the NOEXPAND hint; using an index hint will not work, as they are ignored on views.

Remember, on Enterprise Edition (and hence also on the feature-equal Developer Edition and on DataCenter, which is essentially Enterprise++), the optimizer will automatically consider using indexes on views, regardless of whether the query references the view or the base tables. On lower editions (such as Standard, Workgroup, and Express), the optimizer will never consider using indexes on views, unless the NOEXPAND hint is used to force the use of an index on the view.


I have attached the execution plan of the 5 queries plus an additional query using the NOEXPAND hint. The warning message was not reported with the execution of this query.
Attachments
Hugo_SQL2008R2_Std.sqlplan (7 views, 34.00 KB)
Jagadish Kumar Punnapu
Jagadish Kumar Punnapu
SSC Veteran
SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)

Group: General Forum Members
Points: 252 Visits: 117
Interesting question... thanks...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search