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.
上一篇: 监控Android手机中的网络活动