一、Parallel 1. 用途 強行啓用並行度來執行當前SQL。這個在Oracle 9i之後的版本可以使用,之前的版本現在沒有環境進行測試。也就是說,加上這個說明,可以強行啓用Oracle的多線程處理功能。舉例的話,就像電腦裝了多核的CPU,但大多情況下都不會完全多核同時啓用(2核以上的比較明顯),使用parallel說明,就會多核同時工作,來提高效率。 但本身啓動這個功能,也是要消耗資源與性能的。所有,一般都會在返回記錄數大於100萬時使用,效果也會比較明顯。 2. 語法 /*+parallel(table_short_name,cash_number)*/ 這個可以加到insert、delete、update、select的後面來使用(和rule的用法差不多,有機會再分享rule的用法) 開啓parallel功能的語句是: alter session enable parallel dml; 這個語句是DML語句哦,如果在程序中用,用execute的方法打開。 3. 實例說明 用ERP中的transaction來說明下吧。這個table記錄了所有的transaction,而且每天數據量也算相對比較大的(根據企業自身業務量而定)。假設我們現在要查看對比去年一年當中每月的進、銷情況,所以,一般都會寫成: select to_char(transaction_date,'yyyymm') txn_month, sum( decode( sign(transaction_quantity),1,transaction_quantity,0 ) ) in_qty, sum( decode( sign(transaction_quantity),-1,transaction_quantity,0 ) ) out_qty from mtl_material_transactions mmt where transaction_date >= add_months( to_date( to_char(sysdate,'yyyy')||'0101','yyyymmdd'), -12) and transaction_date <= add_months( to_date( to_char(sysdate,'yyyy')||'1231','yyyymmdd'), -12) group by to_char(transaction_date,'yyyymm') 這個SQL執行起來,如果transaction_date上面有加index的話,效率還算過的去;但如果沒有加index的話,估計就會半個小時內都執行不出來。這是就可以在select 後面加上parallel說明。例如: select /*+parallel(mmt,10)*/ to_char(transaction_date,'yyyymm') txn_month, ... 這樣的話,會大大提高執行效率。如果要將檢索出來的結果insert到另一個表tmp_count_tab的話,也可以寫成: insert /*+parallel(t,10)*/ into tmp_count_tab ( txn_month, in_qty, out_qty ) select /*+parallel(mmt,10)*/ to_char(transaction_date,'yyyymm') txn_month, ... 插入的機制和檢索機制差不多,所以,在insert後面加parallel也會加速的。關於insert機制,這裏暫不說了。 Parallel後面的數字,越大,執行效率越高。不過,貌似跟server的配置還有oracle的配置有關,增大到一定值,效果就不明顯了。所以,一般用8,10,12,16的比較常見。我試過用30,發現和16的效果一樣。不過,數值越大,佔用的資源也會相對增大的。如果是在一些package、function or procedure中寫的話,還是不要寫那麼大,免得佔用太多資源被DBA開K。 4. Parallel也可以用於多表 多表的話,就是在第一後面,加入其他的就可以了。具體寫法如下: /*+parallel(t,10) (b,10)*/ 5. 小結 關於執行效率,建議還是多按照index的方法來提高效果。Oracle有自帶的explan road的方法,在執行之前,先看下執行計劃路線,對寫好的SQL tuned之後再執行。實在沒辦法了,再用parallel方法。Parallel比較邪惡,對開發者而言,不是好東西,會養成不好習慣,導致很多bad SQL不會暴漏,SQL Tuning的能力得不到提升。我有見過某些人create table後,從不create index或primary key,認爲寫SQL時加parallel就可以了。
二、no_index
在生產環境的SQL調優測試過程中經常遇到如下場景:一張表上創建了非常多的索引(不推薦),每一個索引都是針對特定業務查詢而增加的。這極易導致SQL由於個別索引的引入出現性能問題,在這種情況下不能簡簡單單的將索引刪除在解決問題。如何避免SQL不使用特定索引這個問題便擺在了我們面前。
這裏給出通過使用Hint的方法實現強制SQL不走特定索引的方法。
在衆多Oracle Hint中我們選中了“NO_INDEX”。
1.環境準備
1)創建表T
sec@ora10g> create table t as select * from all_objects;
Table created.
sec@ora10g> select count(*) from t;
COUNT(*)
----------
11139
2)在object_name列上創建索引t_idx1
sec@ora10g> create index t_idx1 on t(object_name);
Index created.
3)在object_id列上創建索引t_idx2
sec@ora10g> create index t_idx2 on t(object_id);
Index created.
2.未使用“NO_INDEX”提示時索引使用情況
sec@ora10g> set autotrace on
sec@ora10g> select object_name from t where object_name = 'T';
OBJECT_NAME
------------------------------
T
Execution Plan
----------------------------------------------------------
Plan hash value: 3419373504
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 17 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| T_IDX1 | 1 | 17 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
……省略其他信息輸出……
sec@ora10g> select object_name from t where object_id = 11915;
OBJECT_NAME
------------------------------
T
Execution Plan
----------------------------------------------------------
Plan hash value: 3371054274
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 30 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 30 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T_IDX2 | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
……省略其他信息輸出……
可見以上兩條SQL語句均能正常使用到索引。
3.使用“NO_INDEX”提示時索引使用情況
sec@ora10g> select /*+ NO_INDEX(t t_idx1) */ object_name from t where object_name = 'T';
OBJECT_NAME
------------------------------
T
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 34 | 35 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 2 | 34 | 35 (0)| 00:00:01 |
--------------------------------------------------------------------------
……省略其他信息輸出……
sec@ora10g> select /*+ NO_INDEX(t t_idx2) */ object_name from t where object_id = 11915;
OBJECT_NAME
------------------------------
T
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 60 | 35 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 2 | 60 | 35 (0)| 00:00:01 |
--------------------------------------------------------------------------
……省略其他信息輸出……
此處均顯示爲使用到對應的索引進行檢索數據。我們的實驗目標已經實現。
4.“NO_INDEX”提示的用法補充說明
1)Oracle 10g官方文檔中關於no_index這個HINT的描述信息參見如下鏈接
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements006.htm#SQLRF50411
NO_INDEX Hint
Description of the illustration no_index_hint.gif
(See"Specifying a Query Block in a Hint",tablespec::=,indexspec::=)
TheNO_INDEX
hint instructs the optimizer not to use one or more indexes for the specified table. For example:
SELECT /*+ NO_INDEX(employees emp_empid) */ employee_id
FROM employees
WHERE employee_id > 200;
Each parameter serves the same purpose as in"INDEX Hint"with the following modifications:
If this hint specifies a single available index, then the optimizer does not consider a scan on this index. Other indexes not specified are still considered.
If this hint specifies a list of available indexes, then the optimizer does not consider a scan on any of the specified indexes. Other indexes not specified in the list are still considered.
If this hint specifies no indexes, then the optimizer does not consider a scan on any index on the table. This behavior. is the same as a
NO_INDEX
hint that specifies a list of all available indexes for the table.
TheNO_INDEX
hint applies to function-based, B-tree, bitmap, cluster, or domain indexes. If aNO_INDEX
hint and an index hint (INDEX
,INDEX_ASC
,INDEX_DESC
,INDEX_COMBINE
, orINDEX_FFS
) both specify the same indexes, then the database ignores both theNO_INDEX
hint and the index hint for the specified indexes and considers those indexes for use during execution of the statement.
2)在一條SQL中可以給出多個索引名稱,以便在執行SQL時避免使用這些索引。
sec@ora10g> select /*+ NO_INDEX(t t_idx2 t_idx1) */ object_name from t where object_id = 11915;
OBJECT_NAME
------------------------------
T
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 60 | 35 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 2 | 60 | 35 (0)| 00:00:01 |
--------------------------------------------------------------------------
……省略其他信息輸出……
3)當任何索引都不列出的情況下表示T表上的所有索引都不被使用!
sec@ora10g> select /*+ NO_INDEX (t) */ object_name from t where object_id = 11915;
OBJECT_NAME
------------------------------
T
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 60 | 35 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 2 | 60 | 35 (0)| 00:00:01 |
--------------------------------------------------------------------------
……省略其他信息輸出……
5.小結
在SQL優化中使用Hint提示的方法往往是萬不得已而爲止的行爲。不過本文中提到的方法也可以用於SQL語句的調試和故障排除。靈活使用之。