Solved

SQL 2005 How do you create a Index Over a View

Posted on 2010-09-08
7
322 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 55
Set the max value for a column 7 38
SQL Server 2012 express 24 37
Need help debbuging stored procedure 21 41
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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