What Is Indexing?
Indexing is a way of optimizing the performance of a database by minimizing the number of disk accesses required when a query is processed. It is a data structure technique which is used to quickly locate and access the data in a database. There are two types of indexes in SQL Server:
- Clustered Index
- Non-clustered index
What Is Clustered Index?
A clustered index is a special type of index that reorders the way records in the table are physically stored. 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 only be one clustered index per table, because the data rows themselves can be stored in only one order. The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. When a table has a clustered index, the table is referred to as a clustered table. If a table has no clustered index, its data rows are stored in an unordered structure called a heap.
What You Need To Know About Clustered Index
- A clustered index is a kind of file that sorts the information in the table on their key qualities.
- It can be used to sort the record and store the index in physical memory.
- The size of the clustered index is quite large.
- It stores records in the leaf node of the index.
- In clustered index, clustered key defines order of data within table.
- Clustered indexes are faster to read than non-clustered indexes as data is physically stored in index order.
- Clustered index store pointers to block not data.
- A table can have only one clustered index.
- No extra space is required to store logical structure.
- A clustered index can improve the performance of data retrieval.
- It is slower for insert and update operations than non-clustered index.
- If the table does not have clustered index, it is referred to as a ‘’Heap’’.
- A clustered index always has index Id of 0.
What Is Non-Clustered Index?
A non-clustered is an index where the order of the rows does not match the physical order of the actual data. It is instead ordered by the columns that make up the index. In a non-clustered index, the leaf pages of the index do not contain any actual data, but instead contain pointers to the actual data. These pointers would point to the clustered index data page where the actual data exists or the heap page if no clustered index exists on the table.
A non-clustered index in SQL Server stores the index structure separately from the data physically stored in a table. SQL server allows you to create almost 999 non-clustered index per table. The pointer from an index row in a non-clustered index to a data row is referred to 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 nonkey columns to the leaf level of the non-clustered index to by-pass existing index key limits and execute fully covered, indexed, queries.
What You Need To Know About Non-Clustered Index
- A non-clustered index stores the meta-information in one area and files in another area. The file comprise of the pointers to the location of that information.
- It creates a logical ordering of data rows and uses pointers for accessing the physical data files.
- The size of the non-clustered index is small compared to the clustered index.
- It does not store records in the leaf node of an index that means it takes extra space for data.
- It can work with unique constraints that act as a composite key.
- Non-clustered indexes are slower in read operation as compared to clustered indexes.
- Non-clustered index store both value and a pointer to actual row that holds data.
- A table can have multiple non-clustered indexes.
- Extra space is required to store logical structure.
- Non-clustered index creates the columns that are used in joins.
- It is quicker for insert and update operations than clustered index.
- A table may not have any non-clustered indexes.
- A non-clustered index always contains an index Id>0.
Also Read: Difference Between Strong And Weak Entity
Difference Between Clustered And Non-Clustered Index In Tabular Form
BASIS OF COMPARISON | CLUSTERED INDEX | NON-CLUSTERED INDEX |
Description | A clustered index is a kind of file that sorts the information in the table on their key qualities. | A non-clustered index stores the meta-information in one area and files in another area. The file comprise of the pointers to the location of that information. |
Function | It can be used to sort the record and store the index in physical memory. | It creates a logical ordering of data rows and uses pointers for accessing the physical data files. |
Size | The size of the clustered index is quite large. | The size of the non-clustered index is small compared to the clustered index. |
Storage of Records | It stores records in the leaf node of the index. | It does not store records in the leaf node of an index that means it takes extra space for data. |
Keys | In clustered index, clustered key defines order of data within table. | It can work with unique constraints that act as a composite key. |
Speed Of Reading Operations | Clustered indexes are faster to read than non-clustered indexes as data is physically stored in index order. | Non-clustered indexes are slower in read operation as compared to clustered indexes. |
Storage Of Pointers | Clustered index store pointers to block not data. | Non-clustered index store both value and a pointer to actual row that holds data. |
Table Composition | A table can have only one clustered index. | A table can have multiple non-clustered indexes. |
Storage Of Logical Structure | No extra space is required to store logical structure. | Extra space is required to store logical structure. |
Advantage | A clustered index can improve the performance of data retrieval. | Non-clustered index creates the columns that are used in joins. |
Insert And Update Operations | It is slower for insert and update operations than non-clustered index. | It is quicker for insert and update operations than clustered index. |
Table | If the table does not have clustered index, it is referred to as a ‘’Heap’’. | A table may not have any non-clustered indexes. |
Index Id | A clustered index always has index Id of 0. | A non-clustered index always contains an index Id>0. |