QUERY_REWRITE_INTEGRITY
QUERY_REWRITE_INTEGRITY
Property | Description |
---|---|
Parameter type | String |
Syntax | QUERY_REWRITE_INTEGRITY = { enforced | trusted | stale_tolerated } |
Default value | enforced |
Modifiable | ALTER SESSION , ALTER SYSTEM |
Real Application Clusters | Multiple instances can have different values. |
QUERY_REWRITE_INTEGRITY
determines the degree to which Oracle must enforce query rewriting. At the safest level, Oracle does not use query rewrite transformations that rely on unenforced relationships.
Values:
enforced
Oracle enforces and guarantees consistency and integrity.
trusted
Oracle allows rewrites using relationships that have been declared, but that are not enforced by Oracle.
stale_tolerated
Oracle allows rewrites using unenforced relationships. Materialized views are eligible for rewrite even if they are known to be inconsistent with the underlying detail data.
实验:
TOM书上的样例,我自己做一些实验 大家一起理解
首先我设置 QUERY_REWRITE_INTEGRITY = ENFORCED
TYGER@ORCL>create table emp as select * from scott.emp;
Table created.
TYGER@ORCL>create table dept as select * from scott.dept;
Table created.
TYGER@ORCL>show parameter query
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
query_rewrite_integrity string enforced
TYGER@ORCL>
TYGER@ORCL>create materialized view emp_dept
2 build immediate
3 refresh on demand
4 enable query rewrite
5 as
6 select dept.deptno,dept.dname,count(*)
7 from emp,dept
8 where emp.deptno=dept.deptno
9 group by dept.deptno,dept.dname
10 /
Materialized view created.
TYGER@ORCL>show parameter optimizer
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling integer 2
optimizer_features_enable string 10.2.0.1
optimizer_index_caching integer 0
optimizer_index_cost_adj integer 100
optimizer_mode string ALL_ROWS
optimizer_secure_view_merging boolean TRUE
TYGER@ORCL>set autot traceonly explain
TYGER@ORCL>select count(*) from emp;
COUNT(*)
----------
14
Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
TYGER@ORCL>desc emp
Name Null?Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
TYGER@ORCL>desc dept
Name Null?Type
----------------------------------------- -------- ----------------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
总结: 之所以发生这样的情况是因为知道 DEPTNO 是 DEPT 表的主键。DEPTNO 在表 EMP 中应该是 NOT NULL 的,EMP 表的 DEPTNO 应该是基于 DEPT 表 DEPTNO 列的外键,可是因为我们的表没有这个外键约束存在。而 query_rewrite_integrity 又设置为 enforced ,优化器为了得到完整可靠的数据不得不查询基表而得出终于结果。假如我们加上上面的规则。再看查询结果:
TYGER@ORCL>alter table dept add constraint dept_pk primary key(deptno);
Table altered.
TYGER@ORCL>desc dept
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
TYGER@ORCL>alter table emp
2 add constraint emp_fk_dept
3 foreign key(deptno) references dept(deptno);
Table altered.
TYGER@ORCL>alter table emp modify deptno not null;
Table altered.
TYGER@ORCL>desc emp;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NOT NULL NUMBER(2)
TYGER@ORCL>set autot traceonly explain
TYGER@ORCL>select count(*) from emp;
COUNT(*)
----------
14
Execution Plan
----------------------------------------------------------
Plan hash value: 155013515
--------------------------------------------------------------------------------
----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------------------
----------
| 0 | SELECT STATEMENT | | 1 | 13 | 3 (0)|
00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
| 2 | MAT_VIEW REWRITE ACCESS FULL| EMP_DEPT | 3 | 39 | 3 (0)|
00:00:01 |
--------------------------------------------------------------------------------
----------
Note
-----
- dynamic sampling used for this statement
可见 当我们将完整性约束所有加入后,优化器有了足够的信息知道这条语句通过物化视图能够查询重写,而不用查询基表,实际的运行情况也印证了这一点。
我们在通过一个样例说明 THUSTED 的情况。我们首先将这些约束去除掉,然后加入一行新的数据
TYGER@ORCL>alter table emp drop constraint emp_fk_dept;
Table altered.
TYGER@ORCL>alter table dept drop constraint dept_pk;
Table altered.
TYGER@ORCL>alter table emp modify deptno null;
Table altered.
TYGER@ORCL>desc dept
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
TYGER@ORCL>desc emp;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
// 插入一条违反实际约束行
TYGER@ORCL>insert into emp(empno,deptno) values(1,1);
1 row created.
TYGER@ORCL>exec dbms_mview.refresh('EMP_DEPT');
PL/SQL procedure successfully completed.
TYGER@ORCL>alter materialized view emp_dept consider fresh;
alter materialized view emp_dept consider fresh
*
ERROR at line 1:
ORA-30374: materialized view is already fresh
// 创建一个 novalidate 的约束
TYGER@ORCL>alter table dept
2 add constraint dept_pk primary key(deptno)
3 rely enable novalidate
4 /
Table altered.
TYGER@ORCL>alter table emp
2 add constraint emp_fk_dept
3 foreign key(deptno) references dept(deptno)
4 rely enable novalidate
5 /
Table altered.
TYGER@ORCL>alter table emp modify deptno not null novalidate;
Table altered.
回到原来的查询,假如 query_rewrite_integrity = enforced 的话,那么我们知道因为上述约束实际上是违反真实数据约束的,因此优化器将不会利用物化视图查询重写。
TYGER@ORCL>show parameter query
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
query_rewrite_integrity string enforced
TYGER@ORCL>
TYGER@ORCL>set autot traceonly explain
TYGER@ORCL>select count(*) from emp;
COUNT(*)
----------
16
Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 16 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
而假设设置 query_rewrite_integrity = trusted 的话,那么优化器由于有了我们上述那个 novalidate约束的误导。它并不真实检验数据的完整性,因此还将会利用物化视图查询重写,虽然这样得出的结果是错误的。仅仅要我们让优化器知道有完整性约束的存在。无论约束严格与否,优化器仅仅要为了这个信息就会尽可能地利用物化视图查询重写。
TYGER@ORCL>alter session set query_rewrite_integrity=trusted;
Session altered.
TYGER@ORCL>select count(*) from emp;
COUNT(*)
----------
14
Execution Plan
----------------------------------------------------------
Plan hash value: 155013515
--------------------------------------------------------------------------------
----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------------------
----------
| 0 | SELECT STATEMENT | | 1 | 13 | 3 (0)|
00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
| 2 | MAT_VIEW REWRITE ACCESS FULL| EMP_DEPT | 3 | 39 | 3 (0)|
00:00:01 |
--------------------------------------------------------------------------------
----------
Note
-----
- dynamic sampling used for this statement
而 stale_tolerated 就简单了:
/* 结果 我自己做实验却没有得到的想要的结果: */
TYGER@ORCL>show parameter query
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
query_rewrite_integrity string enforced
// session 级别改动參数
TYGER@ORCL>alter session set query_rewrite_integrity=stale_tolerated;
Session altered.
TYGER@ORCL>show parameter query
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
query_rewrite_integrity string STALE_TOLERATED
TYGER@ORCL>set autot traceonly explain;
TYGER@ORCL>select count(*) from emp;
Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
// system 级别改动參数 仍然如此
TYGER@ORCL>alter system set query_rewrite_integrity=stale_tolerated;
System altered.
TYGER@ORCL>set autot traceonly explain;
TYGER@ORCL>select count(*) from emp;
Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
// 又一次启动数据库 结果依然
TYGER@ORCL>conn / as sysdba
Connected.
SYS@ORCL>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@ORCL>startup
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 1218992 bytes
Variable Size 71304784 bytes
Database Buffers 209715200 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SYS@ORCL>conn tyger/tyger
Connected.
TYGER@ORCL>show parameter query
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
query_rewrite_integrity string STALE_TOLERATED
TYGER@ORCL>set autot traceonly explain
TYGER@ORCL>select count(*) from emp;
Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
尽管结果有点不是非常惬意,可是看完后基本能够了解 query_rewrite_integrity 这个參数了吧
遗留问题:356 行 stale_tolerated 參数设置。 假设看出错误的步骤 欢迎指正。
QUERY_REWRITE_INTEGRITY的更多相关文章
- [terry笔记]物化视图 materialized view基础学习
一.物化视图定义摘录: 物化视图是包括一个查询结果的数据库对像(由系统实现定期刷新数据),物化视图不是在使用时才读取,而是预先计算并保存表连接或聚集等耗时较多的操作结果,这样在查询时大大提高了 ...
- ocp11g培训内部教材_052课堂笔记(042)_体系架构
OCP 052 课堂笔记 目录 第一部分: Oracle体系架构... 4 第一章:实例与数据库... 4 1.Oracle 网络架构及应用环境... 4 2.Oracle 体系结构... 4 3. ...
- Oracle 指数 一个简短的引论
1 索引创建语法: CREATE UNIUQE | BITMAP INDEX <schema>.<index_name> ON <schema>.< ...
- ORACLE数据库维护
ORACLE数据库维护(转)----一篇关于oracle的不错的文章 1. ORACLE数据库启动与关闭 1.1 打开和关闭数据库 (手工)1.1.1 sqlplus连接 1.1.2 打开数据 ...
- 转://云和恩墨的两道Oracle面试题
真题1. 对于一个NUMBER(1)的列,如果查询中的WHERE条件分别是大于3和大于等于4,那么这二者是否等价? 答案:首先对于查询结果而言,二者没有任何区别.从这一点上讲无论是指定大于3还是指定大 ...
- Oracle 参数文件spfile
pfile和spfile 概念 ORACLE中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件,可以分为两种类型.它们是在数据库实例启动时候加载的,决定了数据库的物理结构.内存.数据库的限制 ...
- Oracle物化视图梳理
--物化视图可以分为三种类型:* 包含聚集的物化视图* 只包含连接的物化视图* 嵌套物化视图三种物化视图的快速刷新的限制条件有很大区别,而对于其他方面则区别不大. --物化视图创建方式(Build M ...
- Oracle 学习笔记 (七)
一.数据库的启动 启动数据库的三个阶段: nomount, mount,open mount 阶段:. 1.读参数文件 2.分配内存 3.启动后台进程 4.初始化部分v$视图 mount 阶段: 读参 ...
- ORACLE物化视图(物理视图)
百度文库 http://wenku.baidu.com/view/f78f55c68bd63186bcebbc4b.html ORACLE物化视图 一.------------------------ ...
随机推荐
- go之结构体
一.关于结构体 简述 1.go 语言的切片可以存储同一类型的数据,但是结构体可以为不同项定义不同的数据类型 2.结构体是有一系列具有相同类型或不同类型的数据构成的数据集合 3.因为go 没有类似于类的 ...
- tomcat启动时出现以前删除的项目,导致无法启动
com.sun.faces.config.ConfigureListener contextInitialized 解决: 进入到你自己的tomcat安装目录:C:\Program Files\Apa ...
- C# 获取Enum 描述和值集合
//获取枚举的值 public static IEnumerable<T> GetEnumValues<T>() where T : struct { T[] values = ...
- nodejs __dirname 与 process.cwd()的区别
var cwd = process.cwd(); console.log(cwd); console.log(__dirname); 1 2 3 cwd() 是当前执行node命令时候的文件夹地址 _ ...
- 魅族和三星Galaxy 5.0webView 问题Android Crash Report - Native crash at /system/lib/libc.so caused by webvi
解决办法是当前activity 销毁的时候 webView.destroy(); hine: ConnectedState (when=-2ms what= arg1=!CMD_RSSI_POLL : ...
- 【Oracle】设置快速恢复区及reset快速恢复区
快速恢复区 概念 是一个默认放置所有备份恢复操作有关文件的地方,包括:控制文件在线镜像.在线重做日志.归档日志.外来归档日志.控制文件镜像复制.数据文件镜像复制.RMAN备份片和闪回日志. 如果启用的 ...
- ASP.NET Cache 实现依赖Oracle的缓存策略
ASP.NET 中的缓存提供了对SQL依赖项的支持,也就是说当SQL SERVER数据库中的表或行中的数据被更改后,缓存中的页面就失效,否则,页面输出可一直保留在缓存当中.这确实为程序员提供了方便.但 ...
- CXF-JAX-WS开发(一)入门案例
一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...
- 实验8 标准模板库STL
一.实验目的与要求: 了解标准模板库STL中的容器.迭代器.函数对象和算法等基本概念. 掌握STL,并能应用STL解决实际问题. 二.实验过程: 完成实验8标准模板库STL中练习题,见:http:// ...
- SharePoint Designer 2013 开启新式验证(Modern Authentication)
首先安装office 2013全家桶 再安装SharePoint Designer 2013 安装完之后,去windows检查更新,并把所有需要更新的都更新了 (更新之后我还通过KMS激活了offic ...