Using Covering Indexes To Improve Query Performance - Simple Talk
Using Covering Indexes To Improve Query Performance - Simple Talk
Introduction
There are many ways to measure a database’s
performance. We can look at its overall availability;
that is: are our users able to get the information they
need when they need it? We can consider its
concurrency; how many users can query the
database at one time? We can even watch the
number of transactions that occur per second to
gauge its level of activity.
An indexing refresher
Before taking on the challenge of covering indexes,
let’s take a moment for a brief but very relevant
review of index types and terminology.
Clustered Indexes
A clustered index is an index whose leaf nodes, that
is the lowest level of the index, contain the actual
data pages of the underlying table. Hence the index
and the table itself are, for all practical purposes,
one and the same. Each table can have only one
clustered index. For more information on clustered
indexes, see the Books Online topic “Clustered Index
Structures” (http://msdn.microsoft.com/en-
us/library/ms177443.aspx).
Nonclustered indexes
Nonclustered indexes use a similar methodology to
store indexed data for tables within SQL Server.
However in a nonclustered index, the lowest level of
the index does not contain the data page of the
table. Instead, it contains the information that
allows SQL Server to navigate to the data pages it
needs. For tables that have a clustered index, the
leaf node of the nonclustered index contains the
clustered index keys. In the previous example, the
leaf node of a nonclustered index on the Customers
table would contain the Customer_ID key.
Key Lookups
Looking again at the graphical query execution plan
depicted in Figure 2, notice that the plan not only
includes an Index Seek operation that uses the
ix_Customer_Name nonclustered index, it also
includes a Key Lookup operation.
Covering Indexes
So, if Key Lookups can be detrimental to
performance during query resolution for large result
sets, the natural question is: how can we avoid
them? To answer that question, let’s consider a
query that does not require a Key Lookup.
Summary
By including frequently queried columns in
nonclustered indexes, we can dramatically improve
query performance by reducing I/O costs. Since the
data pages for an nonclustered index are frequently
readily available in memory, covering indexes are the
usually the ultimate in query resolution.