组合索引避免索引扫描后在过滤

SQL>  select corp_org,count(*) from test_2 where end_dt > date'2013-05-01'
  2  group by  corp_org   order by count(*) desc;

CORP_ORG     COUNT(*)
---------- ----------
15601        236734
31170          2549
43530          1309
34001          1238
34210          1049
65100           977
58100           871
65300           566
66740           302
73100           158

10 rows selected.

SQL> select t.agmt_id,t.start_dt,t.end_dt from  test_1 t  where agmt_id='320100629000007';
select t.agmt_id,t.start_dt,t.end_dt from  test_1 t  where agmt_id='320100629000007'
AGMT_ID 				 START_DT   END_DT
---------------------------------------- ---------- ----------
320100629000007 			 2010-12-31 2011-01-22
320100629000007 			 2011-01-22 2011-02-28
320100629000007 			 2011-02-28 2011-08-01
320100629000007 			 2011-08-01 2011-12-06
320100629000007 			 2011-12-06 2012-02-13
320100629000007 			 2012-02-13 2013-04-22
320100629000007 			 2013-04-22 2013-05-31
320100629000007 			 2013-05-31 2013-06-21
320100629000007 			 2013-06-21 2999-12-31

9 rows selected.

SQL> select count(*) from test_2 where end_dt > date'2013-05-01';

  COUNT(*)
----------
    245753

SQL> select count(*) from test_2 ;

  COUNT(*)
----------
   1028959

create index test_2_idx2 on test_2(end_dt,corp_org)

create index test_2_idx1 on test_2(end_dt)


SQL> select * from test_2 where end_dt > date'2013-05-01'
and corp_org='15601'  2  ;

236734 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 4012525493

-------------------------------------------------------------------------------------------
| Id  | Operation		    | Name	  | Rows  | Bytes | Cost (%CPU)| Time	  |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT	    |		  |  5802 |  3518K| 21473   (1)| 00:04:18 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| TEST_2	  |  5802 |  3518K| 21473   (1)| 00:04:18 |
|*  2 |   INDEX RANGE SCAN	    | TEST_2_IDX1 | 58501 |	  |   159   (1)| 00:00:02 |
-------------------------------------------------------------------------------------------

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

   1 - filter("CORP_ORG"='15601')
   2 - access("END_DT">TO_DATE(' 2013-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))


Statistics
----------------------------------------------------------
	  1  recursive calls
	  0  db block gets
     322183  consistent gets
	  0  physical reads
	  0  redo size
   71706482  bytes sent via SQL*Net to client
     174094  bytes received via SQL*Net from client
      15784  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
     236734  rows processed


创建组合索引;

create index test_2_idx2 on test_2(end_dt,corp_org)


SQL> select * from test_2 where end_dt > date'2013-05-01'
and corp_org='15601';  2  

236734 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 2871838242

-------------------------------------------------------------------------------------------
| Id  | Operation		    | Name	  | Rows  | Bytes | Cost (%CPU)| Time	  |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT	    |		  |  5802 |  3518K|  2252   (1)| 00:00:28 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST_2	  |  5802 |  3518K|  2252   (1)| 00:00:28 |
|*  2 |   INDEX SKIP SCAN	    | TEST_2_IDX2 |  5802 |	  |   105   (1)| 00:00:02 |
-------------------------------------------------------------------------------------------

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

   2 - access("END_DT">TO_DATE(' 2013-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
	      AND "CORP_ORG"='15601' AND "END_DT" IS NOT NULL)
       filter("CORP_ORG"='15601')


Statistics
----------------------------------------------------------
	  0  recursive calls
	  0  db block gets
     347799  consistent gets
	  0  physical reads
	  0  redo size
   71706482  bytes sent via SQL*Net to client
     174094  bytes received via SQL*Net from client
      15784  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
     236734  rows processe

SQL> select * from test_2 where end_dt > date'2013-05-01' and corp_org='15601';  2  

236734 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1693794533

-------------------------------------------------------------------------------------------
| Id  | Operation		    | Name	  | Rows  | Bytes | Cost (%CPU)| Time	  |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT	    |		  | 55903 |    33M| 20844   (1)| 00:04:11 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST_2	  | 55903 |    33M| 20844   (1)| 00:04:11 |
|*  2 |   INDEX RANGE SCAN	    | TEST_2_IDX2 | 55903 |	  |   206   (1)| 00:00:03 |
-------------------------------------------------------------------------------------------

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

   2 - access("END_DT">TO_DATE(' 2013-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
	      AND "CORP_ORG"='15601' AND "END_DT" IS NOT NULL)
       filter("CORP_ORG"='15601')


Statistics
----------------------------------------------------------
	  1  recursive calls
	  0  db block gets
     319728  consistent gets
	  0  physical reads
	  0  redo size
   71706482  bytes sent via SQL*Net to client
     174094  bytes received via SQL*Net from client
      15784  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
     236734  rows processed



select /*+ index(test_2 test_2_idx1)*/* from test_2 where end_dt > date'2013-05-01' and corp_org='15601'; 


Execution Plan
----------------------------------------------------------
Plan hash value: 4012525493

-------------------------------------------------------------------------------------------
| Id  | Operation		    | Name	  | Rows  | Bytes | Cost (%CPU)| Time	  |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT	    |		  |  5802 |  3518K| 21473   (1)| 00:04:18 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| TEST_2	  |  5802 |  3518K| 21473   (1)| 00:04:18 |
|*  2 |   INDEX RANGE SCAN	    | TEST_2_IDX1 | 58501 |	  |   159   (1)| 00:00:02 |
-------------------------------------------------------------------------------------------

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

   1 - filter("CORP_ORG"='15601')
   2 - access("END_DT">TO_DATE(' 2013-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))


Statistics
----------------------------------------------------------
	  1  recursive calls
	  0  db block gets
     322183  consistent gets
	  0  physical reads
	  0  redo size
   71706482  bytes sent via SQL*Net to client
     174094  bytes received via SQL*Net from client
      15784  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
     236734  rows processed






posted @ 2014-03-19 19:45  czcb  阅读(229)  评论(0编辑  收藏  举报