原语句的执行计划:
SQL> select t1.object_id,t2.object_type
2 from t1,t2
3 where t1.object_id=t2.object_id
4 and t1.object_type in (select object_type from t3);
Execution Plan
----------------------------------------------------------
Plan hash value: 1500310560
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 236 | 10 (10)| 00:00:01 |
|* 1 | HASH JOIN SEMI | | 4 | 236 | 10 (10)| 00:00:01 |
|* 2 | HASH JOIN | | 4 | 192 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T2 | 4 | 96 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T1 | 10 | 240 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | T3 | 49 | 539 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
目标:将子查询
select object_type from t3 作为fileter条件使用;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 3vhwsnjgsj2cg, child number 0
-------------------------------------
select t1.object_id,t2.object_type from t1,t2 where
t1.object_id=t2.object_id and t1.object_type in (select object_type
from t3)
Plan hash value: 1500310560
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 10 (100)| |
|* 1 | HASH JOIN SEMI | | 4 | 236 | 10 (10)| 00:00:01 |
|* 2 | HASH JOIN | | 4 | 192 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T2 | 4 | 96 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T1 | 10 | 240 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | T3 | 49 | 539 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$5DA710D3
3 - SEL$5DA710D3 / T2@SEL$1
4 - SEL$5DA710D3 / T1@SEL$1
5 - SEL$5DA710D3 / T3@SEL$2
全局写法
SQL> l
1 select /*+ no_unnest(@SEL$2) */ t1.object_id,t2.object_type
2 from t1,t2
3 where t1.object_id=t2.object_id
4* and t1.object_type in (select object_type from t3)
SQL> /
Execution Plan
----------------------------------------------------------
Plan hash value: 4030298478
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 192 | 10 (10)| 00:00:01 |
|* 1 | FILTER | | | | | |
|* 2 | HASH JOIN | | 4 | 192 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T2 | 4 | 96 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T1 | 10 | 240 | 3 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | T3 | 1 | 11 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter( EXISTS (SELECT /*+ NO_UNNEST */ 0 FROM "T3" "T3" WHERE
"OBJECT_TYPE"=:B1))
2 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")
5 - filter("OBJECT_TYPE"=:B1)
直接内部写法
from t1,t2
where t1.object_id=t2.object_id
and t1.object_type in (select /*+ no_unnest(@SEL$2) */ object_type from t3);
等价于
select t1.object_id,t2.object_type
from t1,t2
where t1.object_id=t2.object_id
and t1.object_type in (select /*+ no_unnest */ object_type from t3);
SQL> select t1.object_id,t2.object_type
2 from t1,t2
3 where t1.object_id=t2.object_id
and t1.object_type in (select /*+ no_unnest */ object_type from t3); 4
Execution Plan
----------------------------------------------------------
Plan hash value: 4030298478
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 192 | 10 (10)| 00:00:01 |
|* 1 | FILTER | | | | | |
|* 2 | HASH JOIN | | 4 | 192 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T2 | 4 | 96 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T1 | 10 | 240 | 3 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | T3 | 1 | 11 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter( EXISTS (SELECT /*+ NO_UNNEST */ 0 FROM "T3" "T3" WHERE
"OBJECT_TYPE"=:B1))
2 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")
5 - filter("OBJECT_TYPE"=:B1)
使用QB_NAME 提示
SQL> select /*+ no_unnest(@kfc) */ t1.object_id,t2.object_type
2 from t1,t2
3 where t1.object_id=t2.object_id
4 and t1.object_type in (select /*+ qb_name(kfc) */ object_type from t3);
Execution Plan
----------------------------------------------------------
Plan hash value: 4030298478
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 192 | 10 (10)| 00:00:01 |
|* 1 | FILTER | | | | | |
|* 2 | HASH JOIN | | 4 | 192 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T2 | 4 | 96 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T1 | 10 | 240 | 3 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | T3 | 1 | 11 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter( EXISTS (SELECT /*+ NO_UNNEST QB_NAME ("KFC") */ 0 FROM
"T3" "T3" WHERE "OBJECT_TYPE"=:B1))
2 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")
5 - filter("OBJECT_TYPE"=:B1)