Lecture: Lecture 14 - Physical Access to Data
Related: Physical Access Structures and Access Methods
The difference
A clustered index arranges the actual data in relation to the index key. An unclustered index stores the key values and pointers to data whose physical position is independent of the index order.
The simplest memory rule is: clustered means the data follows the index; unclustered means the index points somewhere else.
One example
Suppose STUDENT(StudentId, Name, Grade) has records with grades 19, 22, 30, 30, 33 and 34.
With a clustered index on Grade, the data pages are arranged approximately in that order:
[T1:19] [T2:22] [T3:30] [T4:30] [T5:33] [T6:34]With an unclustered index, the index may contain:
19 -> T1, 22 -> T2, 30 -> T3, 30 -> T4, 33 -> T5, 34 -> T6but the data file may store the tuples in another order. The pointers still find the rows, but matching rows can require separate page reads.
Query consequences
For Grade BETWEEN 30 AND 34, a clustered index is usually efficient because the matching tuples are physically close. An unclustered index can find the same tuple identifiers, but retrieving many scattered tuples may require more disk I/O.
For a highly selective query that returns only a few rows, an unclustered index can still be efficient. The key issue is how many data pages must be fetched after the index lookup.
Tradeoff
Clustering helps range scans and ordered reading, but preserving the physical order can make insertions and page splits more expensive. Unclustered indexes are flexible secondary access paths, but they do not guarantee that rows found in index order are adjacent in the data file.
The source commonly associates clustered indexes with primary or key-sequenced access and unclustered indexes with secondary indexes. The exact number and maintenance rules depend on the DBMS implementation.
Common confusion
Clustered does not mean that every matching row is permanently adjacent. Inserts, deletes and page splits can change the layout. It means the physical organization follows the index key sufficiently to keep related values together.
Retrieval questions
Why does clustering help a range query? Once the first matching value is found, nearby index entries usually lead to nearby data pages.
Can an unclustered index still be useful? Yes. It can quickly locate a small number of matching rows, even when those rows are scattered.
What does the index store in the unclustered case? It stores pointers or row identifiers to the actual tuples.