SQL SERVER – Indexed View always Use Index on Table

This blog post is written in response to T-SQL Tuesday hosted by Shankar Reddy.

I have been recently writing about Views and their Limitations. While writing this article series, I got inspired to write about SQL Server Quiz Questions. You can view the Quiz Question posted over here.

In SQL Server 2005, a single table can have maximum 249 non clustered indexes and 1 clustered index. In SQL Server 2008, a single table can have maximum 999 non clustered indexes and 1 clustered index. It is widely believed that a table can have only 1 clustered index, and this belief is true. I have some questions for all of you. Let us assume that I am creating view from the table itself and then create a clustered index on it. In my view, I am selecting the complete table itself.

Q1. Does the table use an index created on itself?
Q2. Does the view use an index created on itself?
Q3. Do both the queries use the same index? If yes, why? If no, why not?

The answers are very clear.

The answers are very clear.

A1: Yes
A2: No
A3: Read the rest of the blog! or visit BeyondRelationa.com (where you can win iPAD as well).

Usually, the assumption is that Index on table will use Index on table and Index on view will be used by view. However, that is the misconception. It does not happen this way. In fact, if you notice the image, you will find the both of them (table and view) use both the index created on the table. The index created on the view is not used. The reason for the same as listed in BOL.

The cost of using the indexed view may exceed the cost of getting the data from the base tables, or the query is so simple that a query against the base tables is fast and easy to find. This often happens when the indexed view is defined on small tables. You can use the NOEXPAND hint if you want to force the query processor to use the indexed view. This may require you to rewrite your query if you don’t initially reference the view explicitly. You can get the actual cost of the query with NOEXPAND and compare it to the actual cost of the query plan that doesn’t reference the view. If they are close, this may give you the confidence that the decision of whether or not to use the indexed view doesn’t matter.

10 thoughts on “SQL SERVER – Indexed View always Use Index on Table”

It quite logical, the expanding I mean, when I finally figured it out. If you use index in the view, you need to scan/seek through it and then go to the table and use it’s clustered index to read the actual row data. With table you need only to use table’s clustered index.

I am focusing on your articles since 3 months.These are very impressive.
I am using queris without indexing and result is very slow performance.I am fadded of that kind of results.
I want to use Indexing in my queries for fast results.
Can you tell me where can i find all articles regarding Indexes.
I just want those articles from where i can easily find all articles related on Indexes.
Any guy if can help then please send me on my email below:

Community Initiatives

About Pinal Dave

Pinal Dave is a Pluralsight Developer Evangelist. He has authored 11 SQL Server database books, 17 Pluralsight courses and have written over 3200 articles on the database technology on his blog at a http://blog.sqlauthority.com. Along with 11+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). His past work experiences include Technology Evangelist at Microsoft and Sr. Consultant at SolidQ. Follow @pinaldave
Send Author Pinal Dave
an email at pinal@sqlauthority.com

Email Subscription

Enter your email address to subscribe to this blog and receive notifications of new posts by email.