Clustered vs Non-Clustered Index in Database - Interview Question Answer
Differences between Clustered and Non-Clustered Index in a Database:
1. Clustered Index:
A clustered index physically orders the rows in the table based on the indexed column or columns. There can only be one clustered index per table, and it determines the physical order of data in the table.
2. Non-Clustered Index:
A non-clustered index creates a separate structure that contains the index key values and pointers to the actual data rows. Multiple non-clustered indexes can be created on a table, and they do not affect the physical order of the data.
3. Performance:
Clustered indexes are generally faster for retrieval of large ranges of data or for queries that return ordered results. Non-clustered indexes are more suitable for columns that are frequently used in search conditions.
4. Storage:
In a clustered index, the actual data rows are stored in the leaf nodes of the index, while in a non-clustered index, the leaf nodes contain the index key values and pointers to the data rows.
5. Updates:
Updating values in columns that are part of a clustered index can be slower, as it may require the data rows to be physically re-ordered. Non-clustered indexes do not affect the order of data rows, so updates are generally faster.
Understanding the differences between clustered and non-clustered indexes is crucial for optimizing the performance and storage efficiency of a database.
Please login or Register to submit your answer