Database Reference
In-Depth Information
There are three different ways that SQL Server can read data from the index. The first one is by an ordered scan. Let's
run the query shown in Listing 2-6, which selects the data from the
Customers
table ordered by
CustomerId
column.
Listing 2-6.
Ordered scan query
select Name
from dbo.Customers
order by CustomerId
The data on the leaf level of the index is already sorted based on
CustomerId
column value. As a result, SQL Server
can scan the leaf level of the index from the first to the last page and return the rows in the order they are stored.
SQL Server starts with the root page of the index and reads the first row from there. That row references the
intermediate page with the minimum key value from the table. SQL Server reads that page and repeats the process
until it finds the first page on the leaf level. Then SQL Server starts to read rows one by one, moving through the linked
list of the pages until all rows have been read. Figure
2-8
illustrates this process.
Figure 2-8.
Ordered index scan
The execution plan for the query shows the
Clustered Index Scan
operator with the
Ordered
property set to
true,
as shown in Figure
2-9
.