Oracle 编程艺术--降序索引读书笔记

本文介绍Oracle 11gR2数据库中降序索引的使用及优化效果,包括去除排序操作、多列索引的场景应用等,并通过实际案例对比展示降序索引的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 11gr2数据库的索引默认排序是asc,不是desc。

      准备测试数据  create table test as select rownum as objectid,object_name from all_objects;

      创建默认索引 create index idx_objectid_test on test(objectid);

     select objectid from test; 将得到 1,2,3,4,5... 升序数据。

2.  降序索引的作用1-去掉sort 操作

      若对上面表数据执行

      select *from test order by  objectid asc;  执行计划中没有sort操作,且显示 INDEX FULL SCAN  + TABLE ACCESS BY INDEX ROWID,用时14.42 s

     select *from test order by  objectid desc;  执行计划中没有sort操作,且显示 INDEX FULL SCAN DESCENDING  + TABLE ACCESS BY INDEX ROWID,用时14.12 s

   

 3.  降序索引作用2--多列索引

    下面的例子可以说明。

  1. tony@ORA11GR2> create table t as select * from all_objects;  
  2. Table created.  
  3.   
  4. tony@ORA11GR2> create index t_idx on t(owner,object_type,object_name);  
  5. Index created.  
  6.   
  7. tony@ORA11GR2> exec dbms_stats.gather_table_stats( user'T');  
  8. PL/SQL procedure successfully completed.  
  9.   
  10. tony@ORA11GR2> set autotrace traceonly explain  
  11. tony@ORA11GR2> select owner, object_type, object_name from t  
  12.   2    where owner between 'T' and 'Z'  
  13.   3      and object_type is not null  
  14.   4    order by owner DESC, object_type DESC;  
  15.   
  16. Execution Plan  
  17. ----------------------------------------------------------  
  18. Plan hash value: 2685572958  
  19.   
  20. -------------------------------------------------------------------------------------  
  21. | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  22. -------------------------------------------------------------------------------------  
  23. |   0 | SELECT STATEMENT            |       | 13838 |   567K|   102   (0)| 00:00:02 |  
  24. |*  1 |  INDEX RANGE SCAN DESCENDING| T_IDX | 13838 |   567K|   102   (0)| 00:00:02 |  
  25. -------------------------------------------------------------------------------------  
  26.   
  27. Predicate Information (identified by operation id):  
  28. ---------------------------------------------------  
  29.   
  30.    1 - access("OWNER">='T' AND "OWNER"<='Z')  
  31.        filter("OBJECT_TYPE" IS NOT NULL)  

Oracle会向前读索引,所以查询计划中没有排序。
但是如果有一些列按升序排列,一些列按降序排列,会有不同,如下:

  1. tony@ORA11GR2> select owner, object_type, object_name from t  
  2.   2    where owner between 'T' and 'Z'  
  3.   3      and object_type is not null  
  4.   4    order by owner DESC, object_type ASC;  
  5.   
  6. Execution Plan  
  7. ----------------------------------------------------------  
  8. Plan hash value: 2813023843  
  9.   
  10. -----------------------------------------------------------------------------------  
  11. | Id  | Operation         | Name  | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |  
  12. -----------------------------------------------------------------------------------  
  13. |   0 | SELECT STATEMENT  |       | 13838 |   567K|       |   255   (1)| 00:00:04 |  
  14. |   1 |  SORT ORDER BY    |       | 13838 |   567K|   720K|   255   (1)| 00:00:04 |  
  15. |*  2 |   INDEX RANGE SCAN| T_IDX | 13838 |   567K|       |   102   (0)| 00:00:02 |  
  16. -----------------------------------------------------------------------------------  
  17.   
  18. Predicate Information (identified by operation id):  
  19. ---------------------------------------------------  
  20.   
  21.    2 - access("OWNER">='T' AND "OWNER"<='Z')  
  22.        filter("OBJECT_TYPE" IS NOT NULL)  


可以看到现在必须执行排序操作。
但是如果改用降序索引(使用DESC关键字),有如下结果,可以看到现在不需要排序操作了。

  1. tony@ORA11GR2> drop index desc_t_idx;  
  2. Index dropped.  
  3.   
  4. tony@ORA11GR2> create index desc_t_idx on t(owner desc,object_type,object_name);  
  5. Index created.  
  6.   
  7. tony@ORA11GR2> select owner, object_type, object_name from t  
  8.   2    where owner between 'T' and 'Z'  
  9.   3      and object_type is not null  
  10.   4    order by owner DESC, object_type ASC;  
  11.   
  12. Execution Plan  
  13. ----------------------------------------------------------  
  14. Plan hash value: 2494308350  
  15.   
  16. -------------------------------------------------------------------------------  
  17. | Id  | Operation        | Name       | Rows  | Bytes | Cost (%CPU)| Time     |  
  18. -------------------------------------------------------------------------------  
  19. |   0 | SELECT STATEMENT |            | 13838 |   567K|     4   (0)| 00:00:01 |  
  20. |*  1 |  INDEX RANGE SCAN| DESC_T_IDX | 13838 |   567K|     4   (0)| 00:00:01 |  
  21. -------------------------------------------------------------------------------  
  22.   
  23. Predicate Information (identified by operation id):  
  24. ---------------------------------------------------  
  25.   
  26.    1 - access(SYS_OP_DESCEND("OWNER")>=HEXTORAW('A5FF')  AND  
  27.               SYS_OP_DESCEND("OWNER")<=HEXTORAW('ABFF') )  
  28.        filter(SYS_OP_UNDESCEND(SYS_OP_DESCEND("OWNER"))>='T' AND  
  29.               SYS_OP_UNDESCEND(SYS_OP_DESCEND("OWNER"))<='Z' AND "OBJECT_TYPE" IS NOT  
  30.               NULL)  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值