Solved

SQL 2005 How do you create a Index Over a View

Posted on 2010-09-08
7
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parsing the XML data to SQL Server 4 70
Analysis of table use 7 64
sql query help 2 62
convert null in sql server 12 58
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…

726 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