What is an index in SQL Server?
Understand how indexes can speed up reads, their trade-offs, and what to consider when tuning queries.
Answer
An index is a data structure that helps SQL Server locate rows more efficiently without scanning an entire table in many cases. Indexes are especially useful for columns frequently used in filters, joins, and sorting.
A clustered index determines how the table's data rows are organized at the leaf level of the index. A nonclustered index is a separate structure that contains indexed key values and row locators, and it can include additional columns to cover a query.
Indexes are not free. They consume storage and add work to inserts, updates, and deletes. Good index design depends on the workload, query plans, data distribution, selectivity, and existing indexes. Always measure with representative queries rather than adding indexes blindly.
Code example
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate)
INCLUDE (TotalAmount);Key points to remember
- Inspect actual execution plans and logical reads when tuning queries.
- Consider composite index column order based on query predicates and workload.
- Included columns can help cover a query without expanding the index key.
- Too many or poorly chosen indexes can slow writes and increase maintenance.
Keep practising
Understanding beats memorization.
Explain the concept in your own words, then think through how you would use it in a real project.
AI-powered mock interview practice is planned for a future release.