Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-Clustered Index on a Clustered Index column improves performance?

In SQL Server 2005, the query analyzer has told me many times to create a non-clustered index on a primary ID column of a table which already has a clustered index. After following this recommendation, the query execution plan reports that the query should be faster.

Why would a Non-Clustered index on the same column (with the same sort order) be faster than a Clustered index?

like image 834
Kevin Berridge Avatar asked Sep 03 '08 16:09

Kevin Berridge


People also ask

Can non-clustered index improve performance?

Again, if you are constantly returning data in specific order then create a non-clustered index to speed up the performance. It will also reduce the memory footprint as you will not be required to perform an additional sorting.

Is clustered or nonclustered index faster?

If you want to select only the index value that is used to create and index, non-clustered indexes are faster.

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.


2 Answers

A clustered index has all the data for the table while a non clustered index only has the column + the location of the clustered index or the row if it is on a heap (a table without a clustered index). So if you do a count(column) and that column is indexed with a non clustered index SQL server only has to scan the non clustered index which is faster than the clustered index because more will fit on 8K pages

like image 91
SQLMenace Avatar answered Oct 02 '22 09:10

SQLMenace


I'd guess it would be faster in cases where you don't need the full row data, for example if you're just checking if a row with a given ID does exist. Then a clustered index would be rather huge while a small "one column" index would be much slimmer.

like image 44
BlaM Avatar answered Oct 03 '22 09:10

BlaM