本主题显示使用键优先扫描 的样本查询,该扫描是一种索引扫描,其使用未列为低索引过滤器和高索引过滤器的键。
图: 键优先扫描的部分 SET EXPLAIN 输出
create index idx1 on tab1(c1, c2); select * from tab1 where (c1 > 0) and ( (c2 = 1) or (c2 = 2)) Estimated Cost: 4 Estimated # of Rows Returned: 1 1) pubs.tab1: INDEX PATH (1) Index Keys: c1 c2 (Key-First) (Serial, fragments: ALL) Lower Index Filter: pubs.tab1.c1 > 0 Index Key Filters: (pubs.tab1.c2 = 1 OR pubs.tab1.c2 = 2)
即使在此示例中数据库服务器必须最终读取行数据以返回查询结果,数据库服务器将首先通过应用附加的键过滤器尝试减少可能的行数。 数据库服务器使用索引来应用附加的过滤器 c2 = 1 OR c2 = 2 之后才读取行数据。