查詢轉換-視圖合併
-- 查詢轉換
查詢轉換髮生在進行到執行計劃的開發過程之前,進行完語法和權限檢查之後。優化器爲了決定最終的執行計劃而爲不同的計劃計算成本預估之前。
在查詢通過了語法和權限檢查之後,查詢就進入了轉換爲一系列查詢塊的轉換階段。
查詢塊是通過SELECT關鍵字來定義的。
-- 視圖合併常常發生在當外部查詢塊的謂語包含下列項的時候。
1.能夠在另一個查詢塊的索引中使用的例
2.能夠在另一個查詢塊的分區截斷中所使用的例
3.在一個聯結視圖中能夠限制返回行數的條件。
注意:以下情況會阻止視圖合併的發生:
1.如果一個查詢塊包含解析函數或聚合函數
2.集合運算(如UNION、INTERSERCT、MINUS)
3.ORDER BY 子句或者使用了ROWNUM
以上情況可以通過使用MERGE來強制執行視圖合併。但是結果集可能會與原來的查詢結果不同。
視圖合併行爲是通過一個隱藏參數_complex_view_merging來控制的。在Oracle9及後續版本中該參數的默認值爲TRUE。從Oracle 10版本開始,
轉制後的查詢將會由優化器進行復查,視圖合併以及不合並的查詢計劃所需成本都會被評估。然後優化器就會選擇成本最低的執行計劃。
set line 300
set pages 100
set autotrace traceonly
--a. 進行視圖合併
select *
from emp a
,( select deptno
from dept ) b
where a.deptno = b.deptno(+)
and a.job = 'CLERK' ;
執行計劃
----------------------------------------------------------
Plan hash value: 438843259
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 123 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER| | 3 | 123 | 3 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| EMP | 3 | 114 | 3 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN| PK_DEPT | 1 | 3 | 0 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("A"."JOB"='CLERK')
3 - access("A"."DEPTNO"="DEPTNO"(+))
統計信息
----------------------------------------------------------
1 recursive calls
0 db block gets
10 consistent gets
1 physical reads
0 redo size
1257 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
-- b.沒有進行視圖合併
select *
from emp a
,( select /*+ no_merge*/deptno
from dept ) b
where a.deptno = b.deptno(+)
and a.job = 'CLERK' ;
執行計劃
----------------------------------------------------------
Plan hash value: 1029234253
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 153 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER | | 3 | 153 | 3 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL | EMP | 3 | 114 | 3 (0)| 00:00:01 |
| 3 | VIEW PUSHED PREDICATE | | 1 | 13 | 0 (0)| 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | 3 | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("A"."JOB"='CLERK')
4 - access("DEPTNO"="A"."DEPTNO")
統計信息
----------------------------------------------------------
1 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
1257 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
-- c.無法進行視圖合併
-- ORDER BY
select *
from emp a
,( select deptno
,avg(sal) avg_sal
from emp
group by deptno ) b
where a.deptno = b.deptno
and a.sal >= b.avg_sal ;
執行計劃
----------------------------------------------------------
Plan hash value: 269884559
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 64 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 64 | 8 (25)| 00:00:01 |
| 2 | VIEW | | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 14 | 98 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 532 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."DEPTNO"="B"."DEPTNO")
filter("A"."SAL">="B"."AVG_SAL")
統計信息
----------------------------------------------------------
24 recursive calls
0 db block gets
17 consistent gets
0 physical reads
0 redo size
1503 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
查詢轉換髮生在進行到執行計劃的開發過程之前,進行完語法和權限檢查之後。優化器爲了決定最終的執行計劃而爲不同的計劃計算成本預估之前。
在查詢通過了語法和權限檢查之後,查詢就進入了轉換爲一系列查詢塊的轉換階段。
查詢塊是通過SELECT關鍵字來定義的。
-- 視圖合併常常發生在當外部查詢塊的謂語包含下列項的時候。
1.能夠在另一個查詢塊的索引中使用的例
2.能夠在另一個查詢塊的分區截斷中所使用的例
3.在一個聯結視圖中能夠限制返回行數的條件。
注意:以下情況會阻止視圖合併的發生:
1.如果一個查詢塊包含解析函數或聚合函數
2.集合運算(如UNION、INTERSERCT、MINUS)
3.ORDER BY 子句或者使用了ROWNUM
以上情況可以通過使用MERGE來強制執行視圖合併。但是結果集可能會與原來的查詢結果不同。
視圖合併行爲是通過一個隱藏參數_complex_view_merging來控制的。在Oracle9及後續版本中該參數的默認值爲TRUE。從Oracle 10版本開始,
轉制後的查詢將會由優化器進行復查,視圖合併以及不合並的查詢計劃所需成本都會被評估。然後優化器就會選擇成本最低的執行計劃。
set line 300
set pages 100
set autotrace traceonly
--a. 進行視圖合併
select *
from emp a
,( select deptno
from dept ) b
where a.deptno = b.deptno(+)
and a.job = 'CLERK' ;
執行計劃
----------------------------------------------------------
Plan hash value: 438843259
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 123 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER| | 3 | 123 | 3 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| EMP | 3 | 114 | 3 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN| PK_DEPT | 1 | 3 | 0 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("A"."JOB"='CLERK')
3 - access("A"."DEPTNO"="DEPTNO"(+))
統計信息
----------------------------------------------------------
1 recursive calls
0 db block gets
10 consistent gets
1 physical reads
0 redo size
1257 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
-- b.沒有進行視圖合併
select *
from emp a
,( select /*+ no_merge*/deptno
from dept ) b
where a.deptno = b.deptno(+)
and a.job = 'CLERK' ;
執行計劃
----------------------------------------------------------
Plan hash value: 1029234253
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 153 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER | | 3 | 153 | 3 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL | EMP | 3 | 114 | 3 (0)| 00:00:01 |
| 3 | VIEW PUSHED PREDICATE | | 1 | 13 | 0 (0)| 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | 3 | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("A"."JOB"='CLERK')
4 - access("DEPTNO"="A"."DEPTNO")
統計信息
----------------------------------------------------------
1 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
1257 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
-- c.無法進行視圖合併
-- ORDER BY
select *
from emp a
,( select deptno
,avg(sal) avg_sal
from emp
group by deptno ) b
where a.deptno = b.deptno
and a.sal >= b.avg_sal ;
執行計劃
----------------------------------------------------------
Plan hash value: 269884559
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 64 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 64 | 8 (25)| 00:00:01 |
| 2 | VIEW | | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 14 | 98 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 532 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."DEPTNO"="B"."DEPTNO")
filter("A"."SAL">="B"."AVG_SAL")
統計信息
----------------------------------------------------------
24 recursive calls
0 db block gets
17 consistent gets
0 physical reads
0 redo size
1503 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.