使用索引扫描而不是查找查找

时间:2019-10-20 18:20:58

标签: sql-server non-clustered-index

我有一个具有以下结构的表:

[Vue warn]: Property or method "funds" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

found in

---> <App> at src/App.vue
       <Root>

然后我在位置上放置一个非聚集索引:

CREATE TABLE Article 
(
    id UNIQUEIDENTIFIER PRIMARY KEY,
    title VARCHAR(60),
    content VARCHAR(2000),
    datePosted DATE,
    srcImg VARCHAR(255),
    location VARCHAR(255)
);

运行这样的查询:

CREATE NONCLUSTERED INDEX Articles_location
ON Articles (location);

结果为:“索引扫描(群集)”

运行另一个这样的查询:

select a.content 
from Articles a 
where a.location = 'Japan, Tokyo';

结果为:“索引查找(非聚集)”

因此非聚集索引正在工作。当我按列按附加进行搜索时,为什么不执行查找查询?

  • 表中的总行数为200
  • 此查询检索的行总数为86

1 个答案:

答案 0 :(得分:1)

查询优化器似乎决定扫描表,而不是根据数据的选择性使用索引。

实际上直接引用该表可能比通过索引查找然后执行KeyLookup更快。如果表有更多行(> 10k),则可能不是这种情况。 200个中有86个占40%以上。

select a.content from Articles a where a.location = 'Japan, Tokyo';
-- clustered index scan

select a.location from Articles a where a.location = 'Japan, Tokyo';
-- covering index

Scans vs. Seeks

  

因此,如果我们有一个高度选择性的搜索谓词,则搜索通常是一种更有效的策略;也就是说,如果我们有一个搜索谓词,它消除了表的很大一部分。