oracle view学习小结
视图的定义就是其使用的查询语句,Oracle 将这个定义以文本形式存储
在数据字典中。当用户在 SQL 语句中引用了视图时,Oracle 将进行以
下工作: www.zzzyk.com
1. 将引用了视图的语句与视图的定义语句整合为(merge)一个语句
2. 在共享 SQL 区(shared SQL area)解析(parse)整合后的语句
3. 执行(execute)此语句
接下来用一个实验来证明下:
SQL> alter system flush shared_pool;
SQL> SELECT last_name
2 FROM hr.employees, hr.departments
3 WHERE employees.department_id = departments.department_id AND
4 departments.department_id = 10 AND
5 employees.employee_id = 9876;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1833546154
--------------------------------------------------------------------------------
-------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU
)| Time |
--------------------------------------------------------------------------------
-------------
| 0 | SELECT STATEMENT | | 1 | 15 | 1 (0
)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 15 | 1 (0
)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0
)| 00:00:01 |
--------------------------------------------------------------------------------
-------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("EMPLOYEES"."DEPARTMENT_ID"=10)
2 - access("EMPLOYEES"."EMPLOYEE_ID"=9876)
Statistics
----------------------------------------------------------
202 recursive calls
0 db block gets
290 consistent gets
1 physical reads
0 redo size
337 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
13 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> alter system flush shared_pool;
System altered.
SQL> SELECT last_name
2 from hr.employees_view
3 WHERE employee_id = 9876;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1833546154
--------------------------------------------------------------------------------
-------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU
)| Time |
--------------------------------------------------------------------------------
-------------
| 0 | SELECT STATEMENT | | 1 | 15 | 1 (0
)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 15 | 1 (0
)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0
)| 00:00:01 |
--------------------------------------------------------------------------------
-------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("EMPLOYEES"."DEPARTMENT_ID"=10)
2 - access("EMPLOYEES"."EMPLOYEE_ID"=9876)
Statistics
----------------------------------------------------------
333 recursive calls
0 db block gets
341 consistent gets
0 physical reads
0 redo size
337 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
22 sorts (memory)
0 sorts (disk)
0 rows processed
SQL>
以上实验的sql 执行计划已验证了视图的定义。
视图的使用与基表的索引
oracle 会尽可能地将用户查询及其中所引用视图的定义查询(可能还包
括视图所引用的其他视图)进行整合。Oracle 将优化整合后的语句,就
如同用户提交的语句中没有引用视图一样。因此,无论一列是被视图的
定义引用,还是被用户提交的查询引用,Oracle 都可以使用建于基表列
(base table column)上的索引。