What is the difference between clustered and non-clustered indexes?
sobes.tech AI
Answer from AI
Clustered and non-clustered indexes are ways of organizing data in a database to speed up searches.
-
Clustered Index: determines the physical order of rows in a table. A table can have only one clustered index because data can only be sorted by one key. When searching by a clustered index, data retrieval is faster because they are already ordered.
-
Non-Clustered Index: creates a separate structure that contains keys and pointers to the physical location of data. A table can have multiple non-clustered indexes. When searching, the index is accessed first, then the data is retrieved via the pointer.
Example:
If you have a users table, a clustered index could be on id (primary key), and a non-clustered index could be on the email field for quick email searches.
Thus, the clustered index affects the physical order of data, while the non-clustered index is an additional structure to speed up searches.