Question

This might be an easy one. I'm using SQL Server Management Studio and I would like to place an index on my View. The option for selecting a field as a primary key is greyed out.

Any help would be appreciated! Thanks!

Was it helpful?

Solution

You cannot create an index on a normal view, as a view is simply a saved query that will be run each time the view is referred to. It will simply use the indexes, where appropriate, on the underlying tables the view itself refers to. The data the view returns does not really exist in that form on disk anywhere, thus there's nothing to create an index with.

However, several database platforms have the ability to basically cache the contents of the view on disk, as if it were a table itself. They can detect when the underlying data changes, then update the view. Oracle calls this a materialized view. SQL Server (sticking with obvious names for things) calls this an Indexed View. There are of course limits on what you can do with these sorts of views, I've often found them rather tough to get working for complex queries involving a lot of tables. However, for basic things they should work fairly well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top