Junior — Middle
Can you explain what a non-clustered index in SQL is and how it works?
sobes.tech AI
Answer from AI
A non-clustered index in SQL is a data structure that stores pointers to the table rows, sorted by the value of the indexed column, but the table itself is not reorganized based on this index.
Unlike a clustered index, which determines the physical order of the rows in the table, a non-clustered index is a separate object containing keys and references to the corresponding rows.
How it works:
- When searching by a column with a non-clustered index, the DBMS first accesses the index to find the needed keys.
- Then, full rows are retrieved from the table via links from the index.
Example: if you have an employees table with a non-clustered index on the "LastName" column, searching by last name will be faster because the index is searched first, followed by data retrieval.