【索引】oracle查詢使用索引和不使用索引的比較

散葉涔發表於2012-04-25

前言:關於查詢走不走索引裡面有很多的東西,也分為很多種情況。這裡只是簡單的一個比較,後續會進一步分析。

1、建立表:SQL> create table test1 (id number,c1 varchar2(50),c2 varchar2(50),c3 varchar2(5
0),c4 varchar2(50));

2、模擬插入10000條記錄:

SQL> begin
for i in 1..10000 loop
insert into test1 values(i,'阿斯頓飛就快了','及卡拉加水電費了健康','就愛看lsd減肥辣椒水代理費','阿斯頓飛卡死就到了費勁啊lsd就分開了');
end loop;
end;
/

3、開啟執行計劃,並查詢:

  SQL> set autotrace on;

  SQL> select * from scott.test1 where id=500;

執行計劃
----------------------------------------------------------
Plan hash value: 4122059633

---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 242 | 42 (3)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST1 | 2 | 242 | 42 (3)| 00:00:01 |
---------------------------------------------------------------------------

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

1 - filter("ID"=500)

Note
-----
- dynamic sampling used for this statement


統計資訊
----------------------------------------------------------
171 recursive calls
0 db block gets
283 consistent gets
0 physical reads
0 redo size
708 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processed
4、建立在id上的索引:

SQL> create index i_test1 on test1(id);

5、再次查詢,並檢視執行計劃:

SQL> select * from scott.test1 where id=500;

執行計劃
----------------------------------------------------------
Plan hash value: 550433309

--------------------------------------------------------------------------------
-------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
e |

--------------------------------------------------------------------------------
-------

| 0 | SELECT STATEMENT | | 1 | 121 | 2 (0)| 00:
00:01 |

| 1 | TABLE ACCESS BY INDEX ROWID| TEST1 | 1 | 121 | 2 (0)| 00:
00:01 |

|* 2 | INDEX RANGE SCAN | I_TEST1 | 1 | | 1 (0)| 00:
00:01 |

--------------------------------------------------------------------------------
-------


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

2 - access("ID"=500)

Note
-----
- dynamic sampling used for this statement


統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
712 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

6、可以看到,建立索引後效率有很大提高。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26892340/viewspace-722176/,如需轉載,請註明出處,否則將追究法律責任。

相關文章