如何将Oracle DBMS_ADVANCED_REWRITE与绑定变量一起使用?

时间:2014-12-09 20:09:16

标签: oracle oracle11g oracle10g

我们需要使用绑定变量实现查询重写,因为我们没有选择修改Web应用程序源代码。例如:

BEGIN
     SYS.DBMS_ADVANCED_REWRITE.declare_rewrite_equivalence (
     name             => 'test_rewrite2',
     source_stmt      => 'select COUNT(*) from ViewX where columnA = :1',
     destination_stmt => 'select COUNT(*) from ViewY where columnA = :1',
     validate         => FALSE,
     rewrite_mode     => 'recursive');    
END;

上述命令将导致错误,因为存在绑定变量:

30353. 00000 -  "expression not supported for query rewrite"
*Cause:    The SELECT clause referenced UID, USER, ROWNUM, SYSDATE,
           CURRENT_TIMESTAMP, MAXVALUE, a sequence number, a bind variable,
           correlation variable, a set result, a  trigger return variable, a
           parallel table queue column, collection iterator, a non-deterministic
           date format token RR, etc.
*Action:   Remove the offending expression or disable the REWRITE option on
           the materialized view.

我正在阅读here有一个解决方法,但我无法在网上找到该文档。

你能告诉我这项工作是什么吗?

1 个答案:

答案 0 :(得分:1)

您无法指定绑定参数,但它应该已按您的意愿工作。关键是您作为recursive传递的mode参数。 recursivegeneral模式将拦截涉及表(或视图)的所有语句,忽略过滤器,并将它们转换为目标第二个表(或视图),调整过滤条件来自您原来的陈述。 (如果您已将其定义为TEXT_MATCH,它将检查原始目标语句和目标语句中是否存在相同的过滤器,以便触发转换。)

在下面的示例中,我们可以看到,即使我们没有定义任何绑定条件,过滤器id = 2也是神经无效的;换句话说,它实际上是将SELECT * FROM A1 where id = 2转换为SELECT * FROM A2 where id = 2

set LINESIZE 300

drop table A1;
drop view A2;
drop index A1_IDX;
EXEC SYS.DBMS_ADVANCED_REWRITE.drop_rewrite_equivalence (name => 'test_rewrite');

create table A1 (id number, name varchar2(20));

insert into A1 values(1, 'hello world');
insert into A1 values(2, 'hola mundo');

create index A1_IDX  on A1(id);

select * from A1;

ALTER SESSION SET QUERY_REWRITE_INTEGRITY = TRUSTED;

CREATE OR REPLACE VIEW A2 AS
SELECT id,
       INITCAP(name) AS name
FROM   A1
ORDER BY id desc;


BEGIN
  SYS.DBMS_ADVANCED_REWRITE.declare_rewrite_equivalence (
     name             => 'test_rewrite',
     source_stmt      => 'SELECT * FROM A1',
     destination_stmt => 'SELECT * FROM A2',
     validate         => FALSE,
     rewrite_mode     => 'recursive');
END;
/


select * from A1;

        ID NAME               
---------- --------------------
         2 Hola Mundo          
         1 Hello World         




select * from A1 where id = 2;


        ID NAME               
---------- --------------------
         2 Hola Mundo       


explain plan for
select * from A1 where id = 2;

select * from table(dbms_xplan.display);


PLAN_TABLE_OUTPUT                                                                                                                                                                                                                                                                                          
----------------------------------------------------------------------------------------
Plan hash value: 1034670462                                                                                                                                                                                                                                                                                 

----------------------------------------------------------------------------------------                                                                                                                                                                                                                    
| Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                                    
----------------------------------------------------------------------------------------                                                                                                                                                                                                                    
|   0 | SELECT STATEMENT              |        |     1 |    25 |     2   (0)| 00:00:01 |                                                                                                                                                                                                                    
|   1 |  VIEW                         | A2     |     1 |    25 |     2   (0)| 00:00:01 |                                                                                                                                                                                                                    
|   2 |   TABLE ACCESS BY INDEX ROWID | A1     |     1 |    25 |     2   (0)| 00:00:01 |                                                                                                                                                                                                                    
|*  3 |    INDEX RANGE SCAN DESCENDING| A1_IDX |     1 |       |     1   (0)| 00:00:01 |                                                                                                                                                                                                                    
----------------------------------------------------------------------------------------                                                                                                                                                                                                                    


PLAN_TABLE_OUTPUT                                                                                                                                                                                                                                                                                          
---------------------------------------------------
Predicate Information (identified by operation id):                                                                                                                                                                                                                                                         
---------------------------------------------------                                                                                                                                                                                                                                                         

   3 - access("ID"=2)                                                                                                                                                                                                                                                                                       

Note                                                                                                                                                                                                                                                                                                        
-----                                                                                                                                                                                                                                                                                                       
   - dynamic sampling used for this statement (level=2)                                                                                                                                                                                                                                                     
   - automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold                                                                                                                                                                                                                       

 20 rows selected 

如你所见

  1. 引擎透明地应用转换并返回过滤结果
  2. 最重要的是,应用了过滤器上的转换。过滤器是正确的"推动"进入源表,从A1中提取值。它不是盲目地从A2中提取所有值然后应用过滤器,因此性能得以保留。