组合索引与排序相关细节

set linesize 1000
set autotrace traceonly


drop table t purge;
create table t as select * from dba_objects;
create index idx_t on t (owner,object_id);
alter table t modify owner not null;


select /*+index(a,idx_t)*/ * from t a order by owner desc ,object_type asc;
执行计划
| Id  | Operation                    | Name  | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------- 
|   0 | SELECT STATEMENT             |       | 67342 |    13M|       |  5301   (1)| 00:01:04 | 
|   1 |  SORT ORDER BY               |       | 67342 |    13M|    15M|  5301   (1)| 00:01:04 | 
|   2 |   TABLE ACCESS BY INDEX ROWID| T     | 67342 |    13M|       |  2268   (1)| 00:00:28 | 
|   3 |    INDEX FULL SCAN           | IDX_T | 67342 |       |       |   230   (1)| 00:00:03 | 
---------------------------------------------------------------------------------------------- 
统计信息
----------------------------------------------------------
          0  recursive calls                              
          0  db block gets                                
       2265  consistent gets                              
          0  physical reads                               
          0  redo size                                    
    3559673  bytes sent via SQL*Net to client             
      55428  bytes received via SQL*Net from client       
       5006  SQL*Net roundtrips to/from client            
          1  sorts (memory)                               
          0  sorts (disk)                                 
      75066  rows processed    
有排序

drop index idx_t;
create index idx_t on t(owner desc,object_type asc);


select /*+index(a,idx_t)*/ * from t a order by owner desc ,object_type asc;
执行计划
-----------------------------------------------------------------------------------   
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     | 
------------------------------------------------------------------------------------- 
|   0 | SELECT STATEMENT            |       | 67342 |    13M|  3512   (1)| 00:00:43 | 
|   1 |  TABLE ACCESS BY INDEX ROWID| T     | 67342 |    13M|  3512   (1)| 00:00:43 | 
|   2 |   INDEX FULL SCAN           | IDX_T | 67342 |       |   279   (1)| 00:00:04 | 
------------------------------------------------------------------------------------- 
统计信息
----------------------------------------------------------      
          0  recursive calls                                    
          0  db block gets                                      
      13294  consistent gets                                    
          0  physical reads                                     
          0  redo size                                          
    3502799  bytes sent via SQL*Net to client                   
      55428  bytes received via SQL*Net from client             
       5006  SQL*Net roundtrips to/from client                  
          0  sorts (memory)                                     
          0  sorts (disk)                                       
      75066  rows processed    
      
排序顺序和索引顺序一致, 没有排序      
select /*+index(a,idx_t)*/ * from t a order by owner asc ,object_type desc;
执行计划
----------------------------------------------------------------------------------------------    
| Id  | Operation                    | Name  | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |    
----------------------------------------------------------------------------------------------    
|   0 | SELECT STATEMENT             |       | 67342 |    13M|       |  6544   (1)| 00:01:19 |    
|   1 |  SORT ORDER BY               |       | 67342 |    13M|    15M|  6544   (1)| 00:01:19 |    
|   2 |   TABLE ACCESS BY INDEX ROWID| T     | 67342 |    13M|       |  3512   (1)| 00:00:43 |    
|   3 |    INDEX FULL SCAN           | IDX_T | 67342 |       |       |   279   (1)| 00:00:04 |    
----------------------------------------------------------------------------------------------    
统计信息
----------------------------------------------------------       
          0  recursive calls                                     
          0  db block gets                                       
       3508  consistent gets                                     
          0  physical reads                                      
          0  redo size                                           
    3548163  bytes sent via SQL*Net to client                    
      55428  bytes received via SQL*Net from client              
       5006  SQL*Net roundtrips to/from client                   
          1  sorts (memory)                                      
          0  sorts (disk)                                        
      75066  rows processed    

排序顺序和索引顺序相反,有排序
select /*+index_desc(a,idx_t)*/ * from t a order by owner asc ,object_type desc;
执行计划                                                                              
-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |     
-------------------------------------------------------------------------------------     
|   0 | SELECT STATEMENT            |       | 67342 |    13M|  3512   (1)| 00:00:43 |     
|   1 |  TABLE ACCESS BY INDEX ROWID| T     | 67342 |    13M|  3512   (1)| 00:00:43 |     
|   2 |   INDEX FULL SCAN DESCENDING| IDX_T | 67342 |       |   279   (1)| 00:00:04 |     
-------------------------------------------------------------------------------------     
统计信息
----------------------------------------------------------     
          0  recursive calls                                   
          0  db block gets                                     
      13306  consistent gets                                   
          0  physical reads                                    
          0  redo size                                         
    3502839  bytes sent via SQL*Net to client                  
      55428  bytes received via SQL*Net from client            
       5006  SQL*Net roundtrips to/from client                 
          0  sorts (memory)                                    
          0  sorts (disk)                                      
      75066  rows processed     
没有排序
index_desc


The INDEX_DESC hint instructs the optimizer to use a descending index scan for the specified table. If the statement uses an index range scan and the index is ascending, then Oracle scans the index entries in descending order of their indexed values. In a partitioned index, the results are in descending order within each partition. For a descending index, this hint effectively cancels out the descending order, resulting in a scan of the index entries in ascending order. Each parameter serves the same purpose as in "INDEX Hint".
不是很明白,好像和索引的排序顺序相反
请使用浏览器的分享功能分享到微信等