Oracle查詢轉換(三)外連線檢視合併
檢視的定義及分類參考:http://blog.itpub.net/28536251/viewspace-1871039/
如果檢視定義中包含外連線或者檢視與目標SQL中其他表做外連線,最佳化器會評估檢視合併後的語義是否與目標SQL一致,如果一致就進行檢視合併,也就是外連線檢視合併。那麼什麼情況下語義一致呢,有以下兩種情況:
● 檢視的基表只有一個表。
● 檢視作為外連線的驅動表。
SQL> select employee_id,last_name,salary,department_name from employees_50_vw e,departments d where e.department_id=d.department_id(+);
45 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2408298835
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 45 | 1575 | 4 (0)| 00:00:01 |
|* 1 | HASH JOIN OUTER | | 45 | 1575 | 4 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL | EMPLOYEES | 45 | 855 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 1 | 16 | 1 (0)| 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
SQL> select d.department_name,e.sal_sum from employees_vw e,departments d where e.department_id=d.department_id(+) and department_name='IT';
Execution Plan
----------------------------------------------------------
Plan hash value: 3808327043
---------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8 | 224 | 5 (20)| 00:00:01 |
| 1 | HASH GROUP BY | | 8 | 224 | 5 (20)| 00:00:01 |
| 2 | NESTED LOOPS | | 10 | 280 | 4 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 10 | 280 | 4 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL | DEPARTMENTS | 1 | 21 | 3 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX | 10 | | 0 (0)| 00:00:01 |
| 6 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 10 | 70 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------
以上檢視employees_50_vw和employees_vw的基表都只有一個表,所以不論是作為驅動表還是被驅動表,都可以進行外連線檢視合併。
建立一個有外連線的檢視。
SQL> create view emp_dept_vw as select employee_id,last_name,department_name,location_id from employees e,departments d where e.department_id=d.department_id(+);
View created.
SQL> select last_name,department_name,city from emp_dept_vw e,locations l where e.location_id=l.location_id(+);
107 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 807041693
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 107 | 4494 | 8 (0)| 00:00:01 |
|* 1 | HASH JOIN OUTER | | 107 | 4494 | 8 (0)| 00:00:01 |
|* 2 | HASH JOIN OUTER | | 107 | 3210 | 6 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL | DEPARTMENTS | 27 | 513 | 3 (0)| 00:00:01 |
| 5 | VIEW | index$_join$_002 | 23 | 276 | 2 (0)| 00:00:01 |
|* 6 | HASH JOIN | | | | | |
| 7 | INDEX FAST FULL SCAN| LOC_CITY_IX | 23 | 276 | 1 (0)| 00:00:01 |
| 8 | INDEX FAST FULL SCAN| LOC_ID_PK | 23 | 276 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
此處檢視emp_dept_vw作為驅動表,在執行計劃中沒有檢視emp_dept_vw的名字,表示最佳化器已經做了檢視合併。
SQL> select last_name,department_name,city from emp_dept_vw e,locations l where e.location_id(+)=l.location_id;
122 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3490089952
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 107 | 5992 | 8 (0)| 00:00:01 |
|* 1 | HASH JOIN OUTER | | 107 | 5992 | 8 (0)| 00:00:01 |
| 2 | VIEW | index$_join$_002 | 23 | 276 | 2 (0)| 00:00:01 |
|* 3 | HASH JOIN | | | | | |
| 4 | INDEX FAST FULL SCAN| LOC_CITY_IX | 23 | 276 | 1 (0)| 00:00:01 |
| 5 | INDEX FAST FULL SCAN| LOC_ID_PK | 23 | 276 | 1 (0)| 00:00:01 |
| 6 | VIEW | EMP_DEPT_VW | 107 | 4708 | 6 (0)| 00:00:01 |
|* 7 | HASH JOIN OUTER | | 107 | 3210 | 6 (0)| 00:00:01 |
| 8 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0)| 00:00:01 |
| 9 | TABLE ACCESS FULL | DEPARTMENTS | 27 | 513 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
此處檢視emp_dept_vw作為被驅動表,在執行計劃中有檢視emp_dept_vw的名字,表示最佳化器沒有對檢視進行檢視合併。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2140778/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【SQL】Oracle查詢轉換之檢視合併SQLOracle
- Oracle查詢轉換(一)簡單檢視合併Oracle
- Oracle查詢轉換(二)複雜檢視合併Oracle
- Oracle 12CR2查詢轉換之檢視合併Oracle
- 水煮oracle31----連線查詢&合併查詢Oracle
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- Oracle查詢轉換(四)連線謂詞推入Oracle
- 兩表連線三:合併連線
- 【SQL優化器查詢變換器】檢視合併(View Merging)SQL優化View
- KCSQL SERVER實現連線與合併查詢dinSQLServer
- mysql求交集:UNION ALL合併查詢,inner join內連線查詢,IN/EXISTS子查詢MySql
- oracle 連線查詢Oracle
- 【Oracle】--連線查詢Oracle
- 【janes】多表查詢 外連線
- Oracle 查詢轉換Oracle
- oracle 常用查詢檢視Oracle
- sql 連線查詢例項(left join)三表連線查詢SQL
- 合併查詢
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- 檢視Oracle連線數Oracle
- Oracle 查詢轉換初探Oracle
- Oracle的表連線方法(一)排序合併連線Oracle排序
- oracle連線查詢詳解Oracle
- 隱式轉換影響物化檢視查詢重寫
- Oracle Database 12c查詢最佳化器的缺陷-檢視合併會造成查詢結果不準確OracleDatabase
- 外連線轉換為內連線的情況
- Oracle 查詢轉換-01 or expansionOracle
- Oracle查詢轉換(五)子查詢展開Oracle
- 【PDB】Oracle跨PDB檢視查詢Oracle
- Linux檢視埠併發連線數Linux
- Oracle表連線操作——Merge Sort Join(合併排序連線)Oracle排序
- 3.DQL資料查詢語言(內連線,外連線,自連線)
- 連線查詢
- 【SQL】Oracle查詢轉換之 OR用法SQLOracle
- 【Oracle】如何查詢檢視時使用索引Oracle索引
- linux系統併發連線數檢視Linux
- 查詢重寫對全外連線無效
- MYSQL學習筆記23: 多表查詢(自連線內連線+左右外連線)MySql筆記