首先我們來分別介紹一下這四個hint的概念:
1. ORDERED hint:
ordered hint 指示oracle按照from關(guān)鍵字后的表順序來進行連接。如果沒有及時收集統(tǒng)計信息,查詢優(yōu)化器沒有得到足夠信息,此時你可以自行選擇適當?shù)膇nner及outer表進行連接。ORACLE推薦使用LEADING hint,它比ordered hint有更多的用途。 2. LEADING hint:
leading hint 指示查詢優(yōu)化器使用指定的表作為連接的首表,即驅(qū)動表。
3. USE_NL hint
use_nl hint指示查詢優(yōu)化器使用nested loops方式連接指定表和其他行源,并且將強制指定表作為inner表;但如果此表同時作為outer表則忽略此hint。當使用use_nl時ORACLE推薦與ordered或leading hint搭配使用。
4. INDEX hint
index hint指示查詢優(yōu)化器對指定表使用索引掃描。
hint行為依賴于是否明確指定index:
接下來讓我們看一下具體的示例:
先要準備一些實驗數(shù)據(jù):
create table t2 as select * from dba_objects;
create index indx_t2 on t2(object_id);
create table t3 as select * from dba_objects where rownum<12000;
create index indx_t3 on t3(object_id);
下面開始進行演示,此處會分為幾種情況分別討論:
1. 不使用任何hint,此時t2與t3表通過hash join進行連接。
select count(*) from t3,t2
where t2.object_id=t3.object_id;
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 27 (8)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 26 | | |
|* 2 | HASH JOIN | | 8749 | 222K| 27 (8)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| INDX_T3 | 8748 | 111K| 5 (0)| 00:00:01 |
| 4 | INDEX FAST FULL SCAN| INDX_T2 | 43608 | 553K| 21 (5)| 00:00:01 |
----------------------------------------------------------------------------------
2. 使用ordered與use_nl hint,并且指定t2作為inner表,也就是被驅(qū)動表;因為ordered也是按照t3驅(qū)動t2的順序,因此此時use_nl可以發(fā)揮作用
select /*+ ordered use_nl(t2)*/count(*) from t3,t2
where t2.object_id=t3.object_id;
此處可用leading取代ordered,執(zhí)行計劃則是一樣的,SQL語句如下:
select /*+ leading(t3) use_nl(t2)*/count(*) from t3,t2
where t2.object_id=t3.object_id;
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 8763 (1)| 00:00:56 |
| 1 | SORT AGGREGATE | | 1 | 26 | | |
| 2 | NESTED LOOPS | | 8749 | 222K| 8763 (1)| 00:00:56 |
| 3 | INDEX FAST FULL SCAN| INDX_T3 | 8748 | 111K| 5 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | INDX_T2 | 1 | 13 | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------------
注意此處使用nested loops進行連接,并且t2作為inner表。
3. 使用leading與use_nl hint,leading指定t2作為連接的首表,即驅(qū)動表或者是外表,而use_nl又指定t2作為inner表,也就是被驅(qū)動表;根據(jù)上面所述,如果use_nl指定的表同時作為outer表出現(xiàn),則忽略此hint
select /*+ leading(t2) use_nl(t2)*/count(*) from t3,t2
where t2.object_id=t3.object_id;
此處同樣也可以用ordered替代,執(zhí)行計劃仍然是相同的,SQL語句如下:
select /*+ ordered use_nl(t2)*/count(*) from t2,t3
where t2.object_id=t3.object_id;
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | | 47 (7)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 26 | | | |
|* 2 | HASH JOIN | | 8749 | 222K| 1072K| 47 (7)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| INDX_T2 | 43608 | 553K| | 21 (5)| 00:00:01 |
| 4 | INDEX FAST FULL SCAN| INDX_T3 | 8748 | 111K| | 5 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
此時得出的執(zhí)行計劃與不加任何hint的結(jié)果是一樣的。
|
|