ITPub博客

首页 > 数据库 > Oracle > 浅析query_rewrite_integrity参数

浅析query_rewrite_integrity参数

原创 Oracle 作者:oliseh 时间:2015-08-27 16:56:26 0 删除 编辑

//////////////////////
// 构造测试数据
/////////////////////

create table scott.testp1 (c1 varchar2(10),d1 varchar2(10));
create table scott.testf1 (id11 number,c11 varchar2(10));
insert into scott.testp1 values('a','AAA');
insert into scott.testp1 values('b','BBB');
insert into scott.testp1 values('c','CCC');
alter table scott.testp1 add constraint pk_testp1_c1 primary key(c1) using index tablespace users;

insert into scott.testf1 values(1,'a');
insert into scott.testf1 values(2,'b');
insert into scott.testf1 values(3,'b');
insert into scott.testf1 values(3,'c');

alter table scott.testf1 add constraint fk_testf1_c11 foreign key(c11) references scott.testp1(c1);


create materialized view scott.mvtest build immediate refresh on demand enable query rewrite as select testp1.d1,sum(testf1.id11) from scott.testp1,scott.testf1 where c1=c11 group by d1;


SQL> select * from scott.mvtest;

D1                   SUM(TESTF1.ID11)
-------------------- ----------------
BBB                                 5
AAA                                 1
CCC                                 3


alter table scott.testf1 modify (c11 not null);  <---修改外键为not null,引导optimizer使用query rewrite

////////////////////////
//query_rewrite_integrity=ENFORCED或STALE_TOLERATED的情况下, constraint必须处于enable状态
//才能用上query   rewrite

////////////////////////

SQL> show parameter query_rewrite_integrity

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
query_rewrite_integrity              string                 enforced


SQL> select sum(id11) from scott.testf1;

Execution Plan
----------------------------------------------------------
Plan hash value: 3317803245

----------------------------------------------------------------------------------------
| Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE               |        |     1 |    13 |            |          |
|   2 |   MAT_VIEW REWRITE ACCESS FULL| MVTEST |     3 |    39 |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

alter table scott.testf1 disable constraint fk_testf1_c11;


SQL> select sum(id11) from scott.testf1;


Execution Plan
----------------------------------------------------------
Plan hash value: 222374761

-----------------------------------------------------------------------------
| Id  | Operation          | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |        |     1 |    13 |            |          |
|   2 |   TABLE ACCESS FULL| TESTF1 |     4 |    52 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------

SQL> alter system set query_rewrite_integrity=stale_tolerated scope=memory;


System altered.


SQL> show parameter query_rewrite_integrity;

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
query_rewrite_integrity              string                 STALE_TOLERATED


SQL> select sum(id11) from scott.testf1;


Execution Plan
----------------------------------------------------------
Plan hash value: 222374761

-----------------------------------------------------------------------------
| Id  | Operation          | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |        |     1 |    13 |            |          |
|   2 |   TABLE ACCESS FULL| TESTF1 |     4 |    52 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------

alter table scott.testf1 enable constraint fk_testf1_c11;


SQL> show parameter query_rewrite_integrity;

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
query_rewrite_integrity              string                 STALE_TOLERATED


SQL> select sum(id11) from scott.testf1;


Execution Plan

----------------------------------------------------------
Plan hash value: 3317803245

----------------------------------------------------------------------------------------
| Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE               |        |     1 |    13 |            |          |
|   2 |   MAT_VIEW REWRITE ACCESS FULL| MVTEST |     3 |    39 |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------


//////////////////////

// rely disable constraint的情况下只有query_rewrite_integrity=trusted 才能用上Query rewrite
//
/////////////////////

alter system set query_rewrite_integrity=enforced scope=memory;


SQL> alter table scott.testf1 modify constraint fk_testf1_c11 rely disable;


Table altered.


###rely disable constraint情况下:ENFORCED和STALE_TOLERATED均无法使用查询重写

SQL> show parameter query_rewrite_integrity;

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
query_rewrite_integrity              string                 ENFORCED


SQL> select sum(id11) from scott.testf1;


Execution Plan
----------------------------------------------------------
Plan hash value: 222374761

-----------------------------------------------------------------------------
| Id  | Operation          | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |        |     1 |    13 |            |          |
|   2 |   TABLE ACCESS FULL| TESTF1 |     4 |    52 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------

SQL>  alter system set query_rewrite_integrity=stale_tolerated scope=memory;


System altered.


SQL> show parameter query_rewrite_integrity;

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
query_rewrite_integrity              string                 STALE_TOLERATED


SQL> select sum(id11) from scott.testf1;

Execution Plan
----------------------------------------------------------
Plan hash value: 3317803245

----------------------------------------------------------------------------------------
| Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE               |        |     1 |    13 |            |          |
|   2 |   MAT_VIEW REWRITE ACCESS FULL| MVTEST |     3 |    39 |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------


###rely disable constraint情况下:TRUSTED才能使用查询重写

SQL> alter system set query_rewrite_integrity=trusted scope=memory;


System altered.


SQL> show parameter query_rewrite_integrity;

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
query_rewrite_integrity              string                 TRUSTED


SQL> select sum(id11) from scott.testf1;


Execution Plan

----------------------------------------------------------
Plan hash value: 3317803245

----------------------------------------------------------------------------------------
| Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE               |        |     1 |    13 |            |          |
|   2 |   MAT_VIEW REWRITE ACCESS FULL| MVTEST |     3 |    39 |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)

总结:
QUERY_REWRITE_INTEGRITY=STALE_TOLERATED虽然对于MV内容是否stale没有要求,但在对各种约束的完整性检查方面与ENFORCED遵循同样严格的标准;
QUERY_REWRITE_INTEGRITY=TRUSTED能够信任被标示为rely的约束,无论该约束是否enabled

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/53956/viewspace-1784273/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
不仅仅专注Oracle database技术, member of SHOUG

注册时间:2014-04-06

  • 博文量
    128
  • 访问量
    1641951