简体   繁体   中英

SQL Server : expected clustered index scan, but got non clustered index scan

I have this table:

CREATE TABLE Ta
(
    coda int NOT NULL PRIMARY KEY,
    a2 int UNIQUE   
);

and a SQL select statement:

select *
from Ta

I have a clustered index, the primary key and a non-clustered index, specified by the unique constraint.

Executing the select I get the following execution plan:

在此处输入图片说明

But I'm not sure why.

The data should be on the leaf level, therefore it should scan the leaf level, hence it should do a clustered scan.

EDIT: the table has 10000 rows, coda has values from 9999 to 0 and a2 has values from 0 to 9999.

在此处输入图片说明

The non-clustered index is a covering index for the query. That is, the index contains all of the columns needed to satisfy the query.

The execution plan is showing that SQL Server is using the non-clustered index.

For the given query, it seems like a reasonable execution plan.

If there were some predicate (a WHERE clause condition on a column) or an ORDER BY clause, then we would expect that to influence which index is used.

But in this case, retrieving two columns ( a2 and coda ) for every row in the table with the rows returned in an unspecified order, then a full scan of either index is a suitable plan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM