Indexed Views

  • 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."

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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.

  • 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.

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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.

  • 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.

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (2/13/2012)


    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.

  • Interesting question... thanks...

  • Hugo Kornelis (2/10/2012)


    I hate questions where I have to second-guess the author. Did the author mean that the optimizer would choose to use the indexed view automatically? Or did he mean that you could make the optimizer use it by using hints? I took a 50% chance gamble, and I chose wrong. 🙁

    BTW, now that I am posting anyway - there is one word in the explanation is wrong: "The query optimizer in lower editions will only consider the indexed view if you use the NOEXPAND table hint" (emphasis mine) - this is incorrect. With the NOEXPAND hint, the optimizer will not consider the indexed view; this hint forces the optimizer to use the indexed view. Enterprise edition (and developer edition) is the only place where the optimizer will consider the indexed view as one of multiple options, and choose the cheapest.

    I have to agree. I knew about the NOEXPAND, but reading the question I didn't consider it.

Viewing 11 posts - 31 through 40 (of 40 total)

You must be logged in to reply to this topic. Login to reply