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物化视图 一.------------------------ ...
随机推荐
- jquery的ajax同步异步执行
大家先看一段简单的jquery ajax 返回值的js 代码 function getReturnAjax{ $.ajax({ type:"POST", http:/ ...
- 树莓派-解决apt-get upgrade速度慢的方法[更换阿里云源]
执行 apt-get upgrade 遇到速度慢的原因: 使用国外软件源 解决方法也很简单,将源换为国内环境即可,我选择阿里云 步骤 1.备份为 sources.list sudo cp /etc/a ...
- QS之force(2)
Examples 1) Force input1 to 0 at the current simulator time. force input1 0 2) Force the fourth elem ...
- nvcc fatal : Unsupported gpu architecture 'compute_11'
使用VS编译OpenCV编译源代码时候,对Cmake生成的工程文件编译,会出现 nvcc fatal : Unsupported gpu architecture 'compute_11' 问题.原 ...
- 安卓代码迁移:Program "sh" not found in PATH
Description Resource Path Location Type Program "sh" not found in PATH 参考链 ...
- SLAM: SLAM的发展历程(WIKI)
参考维基百科: https://en.wikipedia.org/wiki/Simultaneous_localization_and_mapping 你们叫他SLAM,我还是习惯叫他三维重建.... ...
- 计算laws的matlab代码
很简单的代码:不过花了codeforge上的10个点,自己写也早写出来了; 代码如下: 文件:calLaws.m function [y,h_v,h_h]=calLaws(x,id,LocalEner ...
- Jquery隐藏和显示Div的控制
html页面代码: <input type="checkbox" name="searchType" value="searchNews&quo ...
- BZOJ 1594: [Usaco2008 Jan]猜数游戏 线段树 + 思维 + 二分
Code: #include<bits/stdc++.h> #define maxn 3000000 using namespace std; void setIO(string s) { ...
- PAT_A1105#Spiral Matrix
Source: PAT A1105 Spiral Matrix (25 分) Description: This time your job is to fill a sequence of N po ...