Introduction
A SQL Server Index is used on a database table for faster data access. In this article, learn what SQL Server Indexes are, why we need them, and how to implement them in SQL Server.
Indexes in SQL Server
SQL Indexes are used in relational databases to retrieve data quickly. They are similar to indexes at the end of the books whose purpose is quickly finding a topic. SQL provides Create Index, Alter Index, and Drop Index commands used to create a new index, update an existing one, and delete an index in SQL Server.
- Data is internally stored in a SQL Server database in "pages" where the size of each page is 8KB.
- A continuous eight pages are called an "Ex. "nt."
- When we create the table, one extent will be allocated for two tables; when it is computed, it is filled with the data. Then another time will be given, and this extent may or may not be continuous to the first extent.
Table Scan
In SQL Server, a system table with the name sysindexes contains information about indexes available on tables in the database. If a table has no index, there will be one row in the sysindexes table related to that table indicating no index on the table when you write a select statement with a condition where clause, the first SQL Server will refer to the "indid" (index id).
Columns of the "Sysindex" table determine whether or not the column on which you write the conditions has an index. When that indid columns, to get an address of the table's first extent and then searches each row of the table for the given value. This process checks the given condition with every table row, called a table scan. A drawback of table scan is that if there is no increase in rows in the table, the time taken to retrieve the data will increase, affecting performance.
Type of Indexes
SQL Server supports two types of indexes:
- Clustered Index
- Non-Clusterd Index.
Clustered Index in SQL Server
A B-Tree (computed) clustered index is the Index that will arrange the rows physically in the memory in sorted order.
An advantage of a clustered index is that searching for a range of values will be fast. A clustered index is internally maintained using a B-Tree data structure leaf node of the btree of the clustered Index will contain the table data; you can create only one clustered Index for a table.
How do we Retrieve the data with clustered Index?
When you write a select statement with a condition in a where clause, then the first SQL Server will refer to the "indid" columns of the "Sysindexes" table and when this column contains the value "1".
Then it indexes the table with a clustered index. In this case, it refers to the columns ." The Root" node of the B-tree of clustered index searches in the b-tree to find the leaf node that contains the first row that satisfies the given conditions and retrieves all the rows that satisfy the given situation that will be in sequence.
Insert and Update with Clustered Index
- Since a clustered index arranges the rows physically in the memory in sorted order, insert and will become slow because the row must be inserted or updated in sorted order.
- Finally, the page into which the row must be inserted or updated, and if free space is not available on the page, create the free space and then perform the insert, update and delete.
- To overcome this problem while creating a clustering index, specify a fill factor, and when you specify a fill factor as 70, then in every page of that table, 70% will fill with data, and the remaining 30% will be left free.
- Since free space is available on every page, the insert and update will be fast.
Nonclustered Index in SQL Server
- A nonclustered index is an index that will not arrange the rows physically in the memory in sorted order.
- An advantage of a nonclustered index is that searching for the values in a range will be fast.
- You can create a maximum of 999 nonclustered indexes on a table, 254 up to SQL Server 2005.
- A nonclustered index is also maintained in a B-Tree data structure. Still, leaf nodes of a B-Tree of the nonclustered Index contain the pointers to the pages that contain the table data and not the table data directly.

Join the conversation! Your thoughts help the community grow.