【INDEX_SS】使用HINT使SQL用索引跳跃扫描(Index Skip Scan)方式快速获取数据

  索引跳跃扫描(Index Skip Scan)可以使用到复合索引的非前缀索引列,达到改善性能的作用,前提是全表扫面的代价高于索引跳跃式扫描的代价。这里给出使用HINT方法使SQL走索引跳跃扫描的方法。

1.初始化环境
1)创建表T
sec@ora10g> create table t(x number,y number);

Table created.

2)初始化1000条数据
sec@ora10g> insert into t select rownum,66 from dual connect by rownum<=1000;

1000 rows created.

sec@ora10g> commit;

Commit complete.

sec@ora10g> select * from t ;

         X          Y
---------- ----------
         1         66
         2         66
         3         66
……省略部分输出……
       998         66
       999         66
      1000         66

1000 rows selected.

3)在表T上创建复合索引
sec@ora10g> create index t_i on t(x,y);

Index created.

4)对表进行分析
sec@ora10g> analyze table t compute statistics;

Table analyzed.

2.使用HINT方法使SQL走索引跳跃扫描
sec@ora10g> explain plan for select /*+ index_ss(t t_i) */ * from t where y=66;

Explained.

sec@ora10g> @?/rdbms/admin/utlxpls.sql

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
Plan hash value: 597150364

-------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------
|   0 | SELECT STATEMENT |      |  1000 |  5000 |  1002   (1)| 00:00:13 |
|*  1 |  INDEX SKIP SCAN | T_I  |  1000 |  5000 |  1002   (1)| 00:00:13 |
-------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("Y"=66)
       filter("Y"=66)

14 rows selected.

3.不使用HINT查看SQL语句的执行计划
sec@ora10g> explain plan for select * from t where y=66;

Explained.

sec@ora10g> @?/rdbms/admin/utlxpls.sql

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3046511974

-----------------------------------------------------------------------------
| Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |  1000 |  5000 |     2   (0)| 00:00:01 |
|*  1 |  INDEX FAST FULL SCAN| T_I  |  1000 |  5000 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("Y"=66)

13 rows selected.

此时SQL使用的是INDEX FAST FULL SCAN方式来获得的数据。

4.小结
  了解并构造每一种SQL语句的执行计划有助于我们深入了解SQL语句的执行方法,进而选择最有效的方法检索和处理数据。

Good luck.

secooler
11.09.13

-- The End --

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/519536/viewspace-707424/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/519536/viewspace-707424/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值