阻止子查询进行 Subquery Unnesting

hint NO_UNNEST 可以禁止CBO 进行 Subquery Unnesting    


SQL> set linesize 200
SQL> set pagesize 200
SQL> explain plan for select * from test1 where owner='SCOTT' and  object_id in(select    object_id from test2 where owner='SCOTT');

已解释。



SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3819917785

----------------------------------------------------------------------------
| Id  | Operation	   | Name  | Rows  | Bytes | Cost (%CPU)| Time	   |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |	   |	 6 |  1422 |   355   (1)| 00:00:05 |
|*  1 |  HASH JOIN SEMI    |	   |	 6 |  1422 |   355   (1)| 00:00:05 |
|*  2 |   TABLE ACCESS FULL| TEST1 |	 6 |  1242 |	 8   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| TEST2 |	14 |   420 |   347   (1)| 00:00:05 |
----------------------------------------------------------------------------

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

   1 - access("OBJECT_ID"="OBJECT_ID")
   2 - filter("OWNER"='SCOTT')
   3 - filter("OWNER"='SCOTT')

Note
-----
   - dynamic sampling used for this statement (level=2)

已选择21行。


1.禁止cbo 进行Subquery Unnesting 



SQL> set linesize 200
SQL> set pagesize 200
SQL> explain plan for select * from test1 where owner='SCOTT' and  object_id in(select    /*+ NO_UNNEST */   object_id from test2 where owner='SCOTT');

已解释。

SQL> select * from table(dbms_xplan.display());

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1896454807

----------------------------------------------------------------------------
| Id  | Operation	   | Name  | Rows  | Bytes | Cost (%CPU)| Time	   |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |	   |	 1 |   207 |  1049   (1)| 00:00:13 |
|*  1 |  FILTER 	   |	   |	   |	   |		|	   |
|*  2 |   TABLE ACCESS FULL| TEST1 |	 6 |  1242 |	 8   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| TEST2 |	 1 |	30 |   347   (1)| 00:00:05 |
----------------------------------------------------------------------------

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

   1 - filter( EXISTS (SELECT /*+ NO_UNNEST */ 0 FROM "TEST2" "TEST2"
	      WHERE "OBJECT_ID"=:B1 AND "OWNER"='SCOTT'))
   2 - filter("OWNER"='SCOTT')
   3 - filter("OBJECT_ID"=:B1 AND "OWNER"='SCOTT')

Note
-----
   - dynamic sampling used for this statement (level=2)

已选择22行。


原文地址:https://www.cnblogs.com/hzcya1995/p/13348725.html