Informix IDS 11细碎经管(918检验)认证指南,第8局部:面向经管员的SQL特征(7)
索引自连接
索引自连接是这样一类索引,假定复合索引中的主要列具有年夜量反复内容,而非主要列具有较好的选择性,则将对盘考举行优化。
在 IDS 的晚期版本中,优化器将对吻合搜索条件的复合索引举行全方位扫描,大约,假定主要列都过错劲条件,则实行接连扫描。
索引自连接将对高度反复的主要列搜索专注组合,然后对搜索获得的专注组合实行较小的盘考,对更具选择性的非主要罗列办过滤。该表在逻辑上实行自连接。
除索引自连接以外,还供给了两种新的指令访问方法:
INDEX_SJ:强逼优化器实行索引自连接AVOID_INDEX_SJ:阻止优化器实行索引自连接
比方,以下表为例,其中的 col1 和 col2 列具有年夜量反复,col3 选择性较好,而这三个列(col1、col2、col3)之上定义了一个复合索引(idx1):
清单 15. 表
CREATE TABLE tab1 (
col1 int,
col2 int,
col3 int);
CREATE INDEX idx1 ON tab1(col1,col2,col3);实行以下盘考:
清单 16. 盘考
SELECT * FROM TAB1
WHERE col1 >= 1 AND col1 <= 2
AND col2 >= 2 AND col2 <= 4
AND col3 >= 40 AND col3 <= 50;
首先,将对主要列 col1 和 col2(高度反复)实行索引扫描,以获得专注组合。然后关于获得的专注组合,运用过滤器 (col1 = col1, col2 = col2, col3 >= 40, col3 <= 50) 实行索引扫描。可以将这种方法视作运用相同索引实行两次索引扫描。
图 1. 运用索引自连接扫描的地域
清单 17. 运用自索引连接设置盘考解释
QUERY:
------
SELECT * FROM TAB1
WHERE col1 >= 1 AND col1 <= 2
AND col2 >= 2 AND col2 <= 4
AND col3 >= 40 AND col3 <= 50
Estimated Cost: 37
Estimated # of Rows Returned: 66
1) informix.tab1: INDEX PATH
(1) Index Keys: col1 col2 col3 (Key-Only) (Serial, fragments: ALL)
Index Self Join Keys (col1 col2 )
Lower bound: informix.tab1.col1 >= 1 AND (informix.tab1.col2 >= 2 )
Upper bound: informix.tab1.col1 <= 2 AND (informix.tab1.col2 <= 4 )
Lower Index Filter: (informix.tab1.col1 = informix.tab1.col1
AND informix.tab1.col2 = informix.tab1.col2 )
AND informix.tab1.col3 >= 40
Upper Index Filter: informix.tab1.col3 <= 50
Index Key Filters: (informix.tab1.col2 <= 4 ) AND
(informix.tab1.col2 >= 2 )
Query statistics:
-----------------
Table map :
----------------------------
Internal name Table name
----------------------------
t1 tab1
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t1 66 66 66 00:00:00 37
图 2. 未运用索引自连接的扫描地域
清单 18. 不运用自索引连接设置盘考解释
QUERY:
------
SELECT { AVOID_INDEX_SJ(TAB1 IDX1)} * FROM TAB1
WHERE col1 >= 1 AND col1 <= 2
AND col2 >= 2 AND col2 <= 4
AND col3 >= 40 AND col3 <= 50
DIRECTIVES FOLLOWED:
AVOID_INDEX_SJ ( tab1 idx1 )
DIRECTIVES NOT FOLLOWED:
Estimated Cost: 208144
Estimated # of Rows Returned: 66
1) informix.tab1: INDEX PATH
(1) Index Keys: col1 col2 col3 (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.tab1.col1 >= 1
AND (informix.tab1.col2 >= 2 )
AND (informix.tab1.col3 >= 40 )
Upper Index Filter: informix.tab1.col1 <= 2
AND (informix.tab1.col3 <= 50 )
AND (informix.tab1.col2 <= 4 )
Index Key Filters: (informix.tab1.col3 <= 50 ) AND
(informix.tab1.col2 <= 4 ) AND
(informix.tab1.col2 >= 2 ) AND
(informix.tab1.col3 >= 40 )
Query statistics:
-----------------
Table map :
----------------------------
Internal name Table name
----------------------------
t1 tab1
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t1 66 66 3500011 00:00:13 208144
版权声明: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追查法律责任。

浙公网安备 33010602011771号