数据库

本类阅读TOP10

·SQL语句导入导出大全
·SQL Server日期计算
·SQL语句导入导出大全
·SQL to Excel 的应用
·Oracle中password file的作用及说明
·MS SQLServer OLEDB分布式事务无法启动的一般解决方案
·sqlserver2000数据库置疑的解决方法
·一个比较实用的大数据量分页存储过程
·如何在正运行 SQL Server 7.0 的服务器之间传输登录和密码
·SQL中两台服务器间使用连接服务器

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
关于Oracle 9i 跳跃式索引扫描(Index Skip Scan)的小测试

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站


在Oracle9i中我们知道能够使用跳跃式索引扫描(Index Skip Scan).然而,能利用跳跃式索引扫描的情况其实是有些限制的.

从Oracle的文档中我们可以找到这样的话:

Index Skip Scans
Index skip scans improve index scans by nonprefix columns.
Often, scanning index blocks is faster than scanning table data blocks.
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.

The number of logical subindexes is determined by the number of distinct values in the initial column.
Skip scanning is advantageous if there are few distinct values in the leading column of the composite
index and many distinct values in the nonleading key of the index.

也可以这样说,优化器根据索引中的前导列(索引到的第一列)的唯一值的数量决定是否使用Skip Scan.

我们首先做个测试:

SQL> CREATE TABLE test AS
  2  SELECT ROWNUM a,ROWNUM-1 b ,ROWNUM-2 c,ROWNUM-3 d,ROWNUM-4 e
  3  FROM all_objects
  4  /

SQL> SELECT DISTINCT COUNT (a) FROM test;

  COUNT(A)
----------
     28251

表已创建。

SQL>
SQL> CREATE INDEX test_idx ON test(a,b,c)
  2  /

索引已创建。

SQL> ANALYZE TABLE test COMPUTE STATISTICS
  2  FOR TABLE
  3  FOR ALL INDEXES
  4  FOR ALL INDEXED COLUMNS
  5  /

表已分析。

SQL> SET autotrace traceonly explain
SQL> SELECT *  FROM test WHERE b = 99
  2  /

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=36 Card=1 Bytes=26)
   1    0 TABLE ACCESS (FULL) OF 'TEST' (Cost=36 Card=1 Bytes=26)

--可见这里CBO选择了全表扫描.

--我们接着做另一个测试:

SQL> drop table test;

表已丢弃。

SQL> CREATE TABLE test
  2  AS
  3  SELECT DECODE(MOD(ROWNUM,2), 0, '1', '2' ) a,
  4                    ROWNUM-1 b,
  5                    ROWNUM-2 c,
  6                    ROWNUM-3 d,
  7                    ROWNUM-4 e
  8    FROM all_objects
  9  /

表已创建。

SQL> set autotrace off
SQL> select distinct a from test;

A
--
1
2

--A列只有两个唯一值

SQL> CREATE INDEX test_idx ON test(a,b,c)
  2  /

索引已创建。


SQL> ANALYZE TABLE test COMPUTE STATISTICS
  2  FOR TABLE
  3  FOR ALL INDEXES
  4  FOR ALL INDEXED COLUMNS
  5  /

表已分析。

SQL> set autotrace traceonly explain
SQL> SELECT *  FROM test WHERE b = 99
  2  /

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=4 Card=1 Bytes=24)
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'TEST' (Cost=4 Card=1 Bytes=24)
   2    1     INDEX (SKIP SCAN) OF 'TEST_IDX' (NON-UNIQUE) (Cost=3 Card=1)

 

Oracle的优化器(这里指的是CBO)能对查询应用Index Skip Scans至少要有几个条件:

1 优化器认为是合适的.
2 索引中的前导列的唯一值的数量能满足一定的条件.
3 优化器要知道前导列的值分布(通过分析/统计表得到)
4 合适的SQL语句
......


更多信息请参考:

http://www.itpub.net/showthread.php?threadid=85948

http://www.cnoug.org/bin/ut/topic_show.cgi?id=608&h=1&bpg=1&age=100

http://www.itpub.net/showthread.php?s=&postid=985602#post985602

Oracle9i Database Performance Tuning Guide and Reference Release 2 (9.2)
Part Number A96533-02

感谢参加讨论的各位高手.




相关文章

相关软件