一、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#SQLRF50411NO_INDEX Hint
Description of the illustration no_index_hint.gif(See"Specifying a Query Block in a Hint",tablespec::=,indexspec::=)
The
NO_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语句的调试和故障排除。灵活使用之。