Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my indexes get dropped when I change an Index'd view's schema?

Server: MS Sql Server 2008

When i create an indexed view .. and i then alter the view's schema, the index's all get dropped.

It's sooo annoying!

Can someone explain why this is? At first I thought that it could be because the fields the index requires are not in the schema any more (we did just alter it, right?) .... but for all the times when the index fields are in the view schema ... it should just leave the index there.

anyways.. rant rant rant ...

just hoping someone might have some inside knowledge on this.

like image 331
Pure.Krome Avatar asked Oct 13 '09 04:10

Pure.Krome


People also ask

Why are indexes dropped?

Some reasons for dropping an index include: The index is no longer required. The index is not providing anticipated performance improvements for queries issued against the associated table. For example, the table might be very small, or there might be many rows in the table but very few index entries.

Do indexes work on views?

Indexes can only be created on views which have the same owner as the referenced table or tables. This is also called an intact ownership-chain between the view and the table(s). Typically, when table and view reside within the same schema, the same schema-owner applies to all objects within the schema.

What happens when you index a view?

Creating a unique clustered index on a view improves query performance because the view is stored in the database in the same way a table with a clustered index is stored. The query optimizer may use indexed views to speed up the query execution.

Why do indexes slow down inserts?

Indexes and constraints will slow inserts because the cost of checking and maintaining those isn't free. The overhead can only be determined with isolated performance testing.


1 Answers

The behavior is by design. From Books Online:

ALTER VIEW can be applied to indexed views; however, ALTER VIEW unconditionally drops all indexes on the view.

When you modify the schema, the clustered index will have to be rebuilt. Since all non-clustered indexes rely on the clustered index, they have to be rebuilt. That's probably why all the indexes are dropped.

like image 51
K. Brian Kelley Avatar answered Sep 19 '22 22:09

K. Brian Kelley