Saturday, October 10, 2015

SQL - Clustered Vs Non Clustered Index

  • Clustered index 
Clustered indexes sort and store the data rows in the table or view based on their key values. These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be sorted in only one order.
Clustered index forms B-tree (Balanced tree) kind structure for fast search
Clustered index has data pages on leaf node
Only one clustered index can be created
why? because a clustered index form a physical ordering of data and we can't have multiple physical ordering of data
If A table has clustered index data stored in sorted manner
If A table not have clustered index data stored in a manner called heap
Sql server automatically creates clustered index on primary key

  • Non clustered index 
Non-clustered indexes have a structure separate from the data rows. A Non-clustered index contains the Non-clustered index key values and each key value entry has a pointer to the data row that contains the key value.
The pointer from an index row in a Non-clustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row locator is a pointer to the row. For a clustered table, the row locator is the clustered index key.
You can add non key columns to the leaf level of the Non-clustered index to by-pass existing index key limits, 900 bytes and 16 key columns, and execute fully covered, indexed, queries.
It is logical ordering of data, not physical, physically data stored in different manner
It has index rows on leaf nodes not data pages those works like pointers to actual data pages
it could be clustered index
249 Non clustered index can be created on a table
Sql server automatically creates non clustered index on any other unique key

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...