How are clustered indexes stored on a hard disk? What is the logical order?
How do non-clustered indexes work?
It means that a clustered index determined the physical order in which records in a table are actually stored. Non-clustered indices are simply lists of key values stored separately that enable fast lookups in other orderings than the clustered/physical ordering.
Quick example: a table with ID
(primary key), FirstName
, LastName
and Car
containing three people: 0=The Stig (Llana), 1=Jeremy Clarkson (DB9), 2=Richard Hammond (911), 3=James May (Lambo) and a clustered index on LastName
and a non-clustered index on Car
would store the actual lines of data in the table in this physical order on disk:
ID FirstName LastName Car
1 Jeremy Clarkson DB9
2 Richard Hammond 911
3 James May Lambo
0 The Stig Llana
The nonclustered index would also store something like:
Car ID
911 2
DB9 1
Lambo 3
Llana 0