Solved

SQL 2005 How do you create a Index Over a View

Posted on 2010-09-08
7
319 Views
Last Modified: 2012-05-10
I can create a View in SQL 2005 by Choosing Views and then create a view.
Now if I want to create a index over that view where do I do that ?
Within my stored procedure ?
I don't want to recreate my view index everytime that stored procedure is called ?
And where is that view index stored in SQL 2005 ?
Looks like creating a index over a view is not as simplified as creating a index over a table.
0
Comment
Question by:thayduck
7 Comments
 
LVL 3

Expert Comment

by:Marbleman
ID: 33631183
As the name "view" states, your are just "viewing" data with it and not storing this selection somewhere.

Therefor you cannot add an index to a a view. I cannot think of a situation that would make this necessary...

However you could do something like that with "SELECT INTO newTable..." but this duplicates the data.
0
 
LVL 5

Accepted Solution

by:
David Christal CISSP earned 42 total points
ID: 33631242
You are correct, indexed views are not as simple as indexed tables.  Here is an article describing the ins and outs of creating indexes on views.  

http://technet.microsoft.com/en-us/library/cc917715.aspx#XSLTsection124121120120

Generally, they are not needed if the related tables are well indexed.  Per the article: "Frequently occurring aggregations and joins are the best candidates for indexed views."
0
 

Author Comment

by:thayduck
ID: 33631257
This IBM site talks about Indexed Views. Thats what got me thinking.

 http://technet.microsoft.com/en-us/library/cc917715.aspx
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:mustaccio
ID: 33631273
There are some limitations as to what views can be indexed, but generally you indeed can create indexes over views. That essentially materializes the view results.

You create an index for a view with a regular CREATE VIEW statement. The first index must be unique and clustered.

You will find more details here: http://technet.microsoft.com/en-us/library/cc917715.aspx
0
 

Author Comment

by:thayduck
ID: 33631347
Sorry dchristal, sent answer before I read your response.
We saw same thing.

I have created 3 indexes over the table that I then created a view over.

I created the view because there are approx 8 flds in the table that could contain a $ amt and if they do then I include record into the report. If all flds are 0 then I ignore the record. I thought it would be better to create a view and only include records that have a $ amt in 1 of these 8 flds. But after I did that I realized I probably lost my index's  since they are over the table and not the view. Thats what made me curious about View Index's..

Will probably re think and put a better Where clause in my query.. and forget about the View..
0
 
LVL 24

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 42 total points
ID: 33631412
When you use a View you don't lose the indexes.  A view is meta-data and still knows about the views.  The only time you lose the power of indexes is if you use a (SELECT query) inside a select query, which creates a Virtual Table.  Then you lose the indexes.

So you should not forget about the view, it should be just fine to use it.  But you could materialize the view with SCHEMABINDING to make it into a psuedo table and only have those columns in the table.  Sometimes this is useful when you have a lot of columns in the table and you don't want to scan or go after the base table.
0
 
LVL 22

Assisted Solution

by:8080_Diver
8080_Diver earned 41 total points
ID: 33631816
Something to bear in mind, though, is that an Indexed View is, indeed, instantiated and changes to the underlying table will result in the recreation of that view.  If you have a lot of INSERTS, UPDATES, or DELETES impacting the underlying table, you can be adding a good bit more load on the database because each of them will trigger the INDEXED VIEW rebuild.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now