Difference between Clustered and Non-clustered is data is physically ordered in Clustered Index whereas in Non-clusteredindex just a pointer to the data is maintained and the data is not physically sorted.
Let us try to understand the same by means of example
I have a table employee with EmployeeID and Name and I insert say these values in table one by one.
1 Bill
2 Derik
3 Andrew
4 Mark
5 John
When there is no index in the table we get the results when we select rows from the table will be
1 Bill
2 Derik
3 Andrew
4 Mark
5 John
When there is non-clustered index then also we receive the same output as above.
If we define a clustered index on Name column then we get the output as
3 Andrew
1 Bill
2 Derik
5 John
4 Mark
Its because we defined a clustered index on the Name column and hence data was sorted according to that and stored in physical disk in the same way.The advantage of this is it works best for range queries as it needs to scan the disk in sequential manner.
So ideal keys for clustered index would be columns on which range queries is used
No comments:
Post a Comment