Oracle stop key算法

  Oracle的stop key是一个很有用的算法,当获取满足条件的数据之后,sql就不往下执行。在实际工作中,我们会有这样的需求,只是判断查询条件是否有满足的数据,并不关心有多少条。下面来做一个例子:
drop table test;
create table test(id number ,name varchar2(100), age number);
insert into test select rownum, 'aaaaaaaaaaa'||rownum, 18  from dual connect by level <1000000;
commit;
create index ind_t_age on test(age);
exec dbms_stats.gather_table_stats(user,'test',cascade => true);
select count(age) from test where age=18;
select count(age) from test where age=18 and rownum=1;
SQL> select count(*) from test where age=18;
已用时间:  00: 00: 00.07
执行计划
----------------------------------------------------------
Plan hash value: 1853573966
-----------------------------------------------------------------------------------
| Id  | Operation             | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |           |     1 |     3 |   537   (2)| 00:00:01 |
|   1 |  SORT AGGREGATE       |           |     1 |     3 |            |          |
|*  2 |   INDEX FAST FULL SCAN| IND_T_AGE |   999K|  2929K|   537   (2)| 00:00:01 |
-----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("AGE"=18)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       1970  consistent gets
          0  physical reads
          0  redo size
        361  bytes sent via SQL*Net to client
        500  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL> select count(*) from test where age=18 and rownum=1;
已用时间:  00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 3902407824
--------------------------------------------------------------------------------
| Id  | Operation          | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |           |     1 |     3 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |           |     1 |     3 |            |          |
|*  2 |   COUNT STOPKEY    |           |       |       |            |          |
|*  3 |    INDEX RANGE SCAN| IND_T_AGE |     1 |     3 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(ROWNUM=1)
   3 - access("AGE"=18)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          3  consistent gets
          0  physical reads
          0  redo size
        359  bytes sent via SQL*Net to client
        500  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

总结:看到stop key的效果了吧,consistent gets从1970降到了3 。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值