如何理解index skip scan

问别人什么是index skip scan的时候,很大回答都局限于,这个执行计划不常见,并且出现在有组合索引,但是前导列不再谓语条件中的查询。仅此而已。

这样的回答,确实基本回答了index skip scan的使用场景。

Index skip scans improve index scans by nonprefix columns. Often, scanning index blocks is faster than scanning table data blocks.
The database may choose an index skip scan when the leading column of the composite index is not specified in a query predicate.
但没有解释,index skip scan是如何实现,在没有前导列的情况下,进行"skip" scan的。

Skip scanning lets a composite index be split logically into smaller subindexes. In skip scanning, the initial column of the composite index is not specified in the query. In other words, it is skipped.
In a skip scan, the number of logical subindexes is determined by the number of distinct values in the leading column.
看到这里,其实就比较好理解了。逻辑上,通过前导列,把组合索引分成几个更小子索引,然后对这些子所以进行扫描,并合并结果。子索引的数目,由前导列的不同值的数目决定。下面是一个例子:

-- For example, assume that you run the following query for a customer in the sh.customers table:

SELECT * FROM sh.customers WHERE cust_email = 'Abbey@company.com';

-- The customers table has a column cust_gender whose values are either M or F. Assume that a composite index exists on the columns (cust_gender, cust_email) that was created as follows:

CREATE INDEX customers_gender_email ON sh.customers (cust_gender, cust_email);

-- portion of the index entries.
F,Wolf@company.com,rowid
F,Wolsey@company.com,rowid
F,Wood@company.com,rowid
F,Woodman@company.com,rowid
F,Yang@company.com,rowid
F,Zimmerman@company.com,rowid
M,Abbassi@company.com,rowid
M,Abbey@company.com,rowid

-- The database can use a skip scan of this index even though cust_gender is not specified in the WHERE clause.

-- When searching for the record for the customer whose email is  bbey@company.com, the database searches the subindex with the value First and then searches the subindex with the value M. Conceptually, the database processes the query as follows:

SELECT * FROM sh.customers WHERE cust_gender = 'F' 
  AND cust_email = 'Abbey@company.com'
UNION ALL
SELECT * FROM sh.customers WHERE cust_gender = 'M'
  AND cust_email = 'Abbey@company.com';

转载于:https://my.oschina.net/huayd/blog/133788

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值