查询块的几种简单使用

原语句的执行计划:
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)


直接内部写法

.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(@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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值