Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does creating a nonclustered index on a SQL Server 2005 table prevent selects?

I'd like to create an index on a view I have but I need to make sure the data can still be read while the index is being created. I was reading an article that suggested that when creating a nonclustered index that the data is still readable if you specify the ONLINE=ON option (example below):

CREATE UNIQUE CLUSTERED INDEX CLUST_IDX_SQLTIPS
ON SQLTips (tip) with (ONLINE=ON) 

Am I understanding this correctly? Is there any potential issues I should be aware of before I create indexes on a view that needs to be readable while I create my index?

like image 208
Abe Miessler Avatar asked Dec 16 '09 17:12

Abe Miessler


People also ask

What happens when you create a non-clustered index?

Generally, nonclustered indexes are created to improve the performance of frequently used queries not covered by the clustered index or to locate rows in a table without a clustered index (called a heap). You can create multiple nonclustered indexes on a table or indexed view.

Does creating a nonclustered index lock table?

An offline index operation that creates a nonclustered index acquires a Shared (S) lock on the table. This prevents updates to the underlying table but allows read operations, such as SELECT statements.

What will happen when a non-clustered index is created on a column of the table?

Non-Clustered Indexes A non-clustered index doesn't sort the physical data inside the table. In fact, a non-clustered index is stored at one place and table data is stored in another place. This is similar to a textbook where the book content is located in one place and the index is located in another.

What are the disadvantages of creating an index on table?

Its disadvantages include increased disk space, slower data modification, and updating records in the clustered index.


2 Answers

Online index creation and rebuild are available only on Enterprise Edition. See How Online Index Operations Work and Guidelines for Performing Online Index Operations.

There are some restrictions, most notable ones being:

  • clustered index must be created/rebuilt offline if they contain any BLOB fields (image, ntext, text, varchar(max), nvarchar(max), varbinary(max), and xml).
  • intitial clustered index on a view must be created offline.

You must ensure your database have enough space to perform the online index operation, as it requires about 1.5 times the size of the table in addition to the current size. During the online index creation the table exist twice in the database, hence the extra space needed.

Since your case falls in the excluded category (initial clustered index on a view) then you need not worry about online indexes. You must use an offline index operation.

BTW you must also be aware that indexed views are considered by the optimizer only in Enterprise Edition. On lower editions one must specify the NOEXPAND clause in the view to leverage a possible index on the view.

like image 174
Remus Rusanu Avatar answered Oct 18 '22 16:10

Remus Rusanu


There's more information on the msdn articles about CREATE INDEX and online index operations that has lots of information about it.

There should be no issues with it, if you're only doing SELECTs and UPDATEs. Not so sure about backups, maybe best to try it on a test system and see?

like image 26
thecoop Avatar answered Oct 18 '22 16:10

thecoop