• 分页语句 where 条件列和Order by 列 引导列的问题


    SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
    
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    SQL_ID	599jjhj1rar9u, child number 0
    -------------------------------------
    select rownum - 1 as xuhao, ggbt, czsj, fj, id	 from (select  *
       from mbank.t_ises_gg t	   where t.status = '1' 	   and
    to_date(t.zzrq, 'YYYY-MM-DD') >=		to_date('20150310',
    'YYYY-MM-DD')	       order by czsj desc)  where rownum <= 4
    
    Plan hash value: 747778291
    
    --------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation		       | Name		| Starts | E-Rows | A-Rows |   A-Time	| Buffers |  OMem |  1Mem | Used-Mem |
    --------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT	       |		|      1 |	  |	 4 |00:00:00.01 |      10 |	  |	  |	     |
    |*  1 |  COUNT STOPKEY		       |		|      1 |	  |	 4 |00:00:00.01 |      10 |	  |	  |	     |
    |   2 |   VIEW			       |		|      1 |	2 |	 4 |00:00:00.01 |      10 |	  |	  |	     |
    |*  3 |    SORT ORDER BY STOPKEY       |		|      1 |	2 |	 4 |00:00:00.01 |      10 |  4096 |  4096 | 4096  (0)|
    |   4 |     TABLE ACCESS BY INDEX ROWID| T_ISES_GG	|      1 |	2 |	29 |00:00:00.01 |      10 |	  |	  |	     |
    |*  5 |      INDEX RANGE SCAN	       | T_ISES_GG_IDX1 |      1 |	1 |	29 |00:00:00.01 |	1 |	  |	  |	     |
    --------------------------------------------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter(ROWNUM<=4)
       3 - filter(ROWNUM<=4)
       5 - access("T"."SYS_NC00019$">=TO_DATE('20150310','YYYY-MM-DD') AND "T"."STATUS"='1' AND "T"."SYS_NC00019$" IS NOT NULL)
           filter("T"."STATUS"='1')
    
    
    28 rows selected.
    
    
    
     create index mbank.t_ises_gg_idx1 on mbank.t_ises_gg(to_date(zzrq, 'YYYY-MM-DD'),czsj,status)
    
    
    此时创建的索引顺序为,where 条件列再前
    
    
    
    
    创建如下索引:
     create index mbank.t_ises_gg_idx1 on mbank.t_ises_gg(czsj,to_date(zzrq, 'YYYY-MM-DD'),status);
    
    order by 列在前
    
    
    SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
    
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    SQL_ID	599jjhj1rar9u, child number 0
    -------------------------------------
    select rownum - 1 as xuhao, ggbt, czsj, fj, id	 from (select  *
       from mbank.t_ises_gg t	   where t.status = '1' 	   and
    to_date(t.zzrq, 'YYYY-MM-DD') >=		to_date('20150310',
    'YYYY-MM-DD')	       order by czsj desc)  where rownum <= 4
    
    Plan hash value: 3991473710
    
    ----------------------------------------------------------------------------------------------------------
    | Id  | Operation		      | Name	       | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT	      | 	       |      1 |	 |	4 |00:00:00.01 |       4 |
    |*  1 |  COUNT STOPKEY		      | 	       |      1 |	 |	4 |00:00:00.01 |       4 |
    |   2 |   VIEW			      | 	       |      1 |      2 |	4 |00:00:00.01 |       4 |
    |   3 |    TABLE ACCESS BY INDEX ROWID| T_ISES_GG      |      1 |      2 |	4 |00:00:00.01 |       4 |
    |*  4 |     INDEX FULL SCAN DESCENDING| T_ISES_GG_IDX1 |      1 |      1 |	4 |00:00:00.01 |       2 |
    ----------------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter(ROWNUM<=4)
       4 - access("T"."STATUS"='1' AND "T"."SYS_NC00019$">=TO_DATE('20150310','YYYY-MM-DD'))
           filter(("T"."STATUS"='1' AND "T"."SYS_NC00019$">=TO_DATE('20150310','YYYY-MM-DD')))
    
    
    26 rows selected.
    
    此时只访问了4条记录。
    
    验证了 分页SQL 引导列不一定是where 条件列

  • 相关阅读:
    吴裕雄--天生自然 诗经:天净沙·秋思
    阿里云Kubernetes服务
    一探究竟:善用 MaxCompute Studio 分析 SQL 作业
    MaxCompute Studio 使用入门
    AI 一体机,满足新时代的新需求
    OSS重磅推出OSS Select——使用SQL选取文件的内容
    如何将DynamoDB的数据增量迁移到表格存储
    多隆:淘宝第一行代码撰写者的程序世界
    专访阿里巴巴量子实验室:最强量子电路模拟器“太章”到底强在哪?
    饿了么CTO张雪峰:允许90后的技术人员“浮躁“一点
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351837.html
Copyright © 2020-2023  润新知