SQL Server Covering Indexes Explained
A covering index is an index that contains all the columns required by a particular query. When designed appropriately, it can allow SQL Server to answer the query from the index without repeatedly looking up the base table.
๐ The Problem
SELECT EmployeeId, EmployeeName, DepartmentId
FROM Employees
WHERE DepartmentId = 10;
An index on DepartmentId may help locate matching rows, but SQL Server may still need additional lookups to retrieve other selected columns.
๐ฆ Included Columns
SQL Server allows non-key columns to be included in a nonclustered index:
CREATE INDEX IX_Employees_Department
ON Employees (DepartmentId)
INCLUDE (EmployeeId, EmployeeName);
The key column supports the search predicate while the included columns provide additional data needed by the query.
โก Why Can It Be Faster?
If the index contains everything the query needs, SQL Server may avoid a separate lookup into the clustered index or heap.
This can reduce I/O for suitable workloads, especially when the query runs frequently.
โ ๏ธ Don't Include Everything
A very wide covering index is not automatically better. Larger indexes require more storage and can increase the cost of inserts, updates, deletes, and index maintenance.
๐งช Check the Actual Execution Plan
Use the actual execution plan and query performance metrics to determine whether a key lookup is expensive and whether an index change actually improves the workload.
๐ง Key Takeaway
A covering index can contain the columns needed by a query and reduce expensive lookups, but it should be designed around real workload patterns.
Continue with ๐ข๏ธ SQL Server Indexing Explained.
Ask AlgoLassi and get an answer plus the tutorials worth studying next.
๐ฌ Comments
Sign in with Google to publish immediately, or comment anonymously and wait for approval.
Comments will appear here when available.