opt_param 隐含参数修改
Sort MERGE join 排序合并
Goal
Query performs badly because of Sort-Merge Join.
When the pga_aggregate_target is increased, then the optimizer would favour for SM join which could be time consuming. So it is better to disable the SM join so that the optimizer would not favour SM joins in any circumstances.
SM Join can be disabled using the following parameter:
"_optimizer_sortmerge_join_enabled"
Solution
NOTE: In the images and/or the document content below, the user information and data used represents fictitious data from the Oracle sample schema(s) or Public Documentation delivered with an Oracle database product. Any similarity to actual persons, living or dead, is purely coincidental and not intended in any manner.
This parameter is available from 9.2.0 onwards. The value of the parameter is true by default. This parameter would make the optimizer to select the sort merge joins for SQL statement.
If the sort merge joins selected by the optimizer is consuming too much of resources and cpu time, then you can consider disabling the sort merge joins using the above said parameter.
Example:
SQL> explain plan for select a.empno,a.ename,b.deptno from emp a , dept b where a.deptno<b.deptno;
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 48 | 7 |
| 1 | MERGE JOIN | | 3 | 48 | 7 |
| 2 | SORT JOIN | | 4 | 12 | 1 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | 12 | 1 |
|* 4 | SORT JOIN | | 14 | 182 | 6 |
| 5 | TABLE ACCESS FULL| EMP | 14 | 182 | 2 |
---------------------------------------------------------------
The presence of sort merge joins for a query is found in the execution plan with sort join and merge join sort join 中的join是不是多了, sort merge join 排序合并 in the explain plan output.
This can be disabled at system level or session level.
i ) Disabling the sort merge join at system level
SQL> alter system set "_optimizer_sortmerge_join_enabled"=false;
Note: This can be done only from 10g onwards. Prior to 10g , the parameter has to be modified in init.ora and requires database bounce back.
SQL> explain plan for select a.empno,a.ename,b.deptno from emp a , dept b where a.deptno<b.deptno;
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 48 | 4 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 3 | 48 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 182 | 3 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | PK_DEPT | 1 | 3 | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------
ii ) Disabling the sort merge join using hints
SQL> explain plan for select /*+ opt_param('_optimizer_sortmerge_join_enabled','false') */ a.empno,a.ename,b.deptno from emp a , dept b where a.deptno<b.deptno;
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 48 | 4 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 3 | 48 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 182 | 3 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | PK_DEPT | 1 | 3 | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------
Note: This can be done only from 10g onwards. Prior to 10g, there is no any hints like opt_param.