问题描述
我无法获得选择语句(下面脚本中的最后一行)来查询重写以使用物化视图。不幸的是,我没有 REWRITE_TABLE
,所以我不能使用 dbms_view.explain_rewrite
。我正在使用执行计划来确认它没有被重写以使用物化视图。所有参数(参数显示在下面脚本的末尾附近)和子句似乎都已到位。即使使用了 REWRITE
提示,它仍然没有重写。我错过了什么?提前致谢。
drop table A;
create table A
(
x number not null,y char(1 byte) not null
)
;
insert into A (x,y) select level,chr(mod(level-1,10) + ascii('A'))
from dual connect by level <= 1000
;
create unique index IU_A_x ON A (x);
create index IX_A_y ON A (y);
alter table A
add (
constraint PK_A primary key ( x )
rely
using index IU_A_x
enable validate
)
;
--analyze table statistics for A
drop table B;
create table B
(
x number not null,z char(1 byte) not null
)
;
insert into B (x,z) select mod(level,1000)+1,26) + ascii('A'))
from dual connect by level <= 1000000
;
create index IX_B_x ON B (x);
create index IX_B_z ON B (z);
alter table B
add (
constraint R_B_x foreign key ( x )
references A (x)
rely
enable validate
)
;
--analyze table statistics for B
drop materialized view MV;
create materialized view MV
(
y,z
)
build immediate
refresh force on demand
with primary key
using trusted constraints
enable query rewrite
as
select
A.y,B.z from A join B on A.x = B.x
;
create index IX_MV_y ON MV (y);
create index IX_MV_z ON MV (z);
--analyze table statistics for MV
set serveroutput on;
show parameters optimizer_mode; --all_rows
show parameters query_rewrite_enabled; -- true
show parameters query_rewrite_integrity; --enforced
select /*+ rewrite */ A.y,B.z from A join B on A.x = B.x where y = 'A' and z = 'Z';
解决方法
我无法真正解释为什么,但我做了一个观察,可以让您解决问题。
这是您的explain_mview
MV
结果
exec dbms_mview.explain_mview(q'[select A.y,B.z from A join B on A.x = B.x]');
SELECT capability_name,possible,SUBSTR(related_text,1,8)
AS rel_text,SUBSTR(msgtxt,60) AS msgtxt
FROM MV_CAPABILITIES_TABLE
WHERE capability_name like '%REWRITE%'
ORDER BY seq;
CAPABILITY_NAME P REL_TEXT MSGTXT
------------------------------ - -------- ------------------------------------------------------------
REWRITE Y
REWRITE_FULL_TEXT_MATCH Y
REWRITE_PARTIAL_TEXT_MATCH Y
REWRITE_GENERAL N the reason why the capability is disabled has escaped analys
REWRITE_PCT N general rewrite is not possible or PCT is not possible on an
问题是 REWRITE_GENERAL = 'N'
中的 IMO
如果仅使用 explain_mview
(= Oracle 中的普通旧连接)重复相同的 POJO
,您将看到不同的结果。
truncate table mv_capabilities_table;
exec dbms_mview.explain_mview(q'[select A.y,B.z from A,B where A.x = B.x]');
CAPABILITY_NAME P REL_TEXT MSGTXT
------------------------------ - -------- ------------------------------------------------------------
REWRITE Y
REWRITE_FULL_TEXT_MATCH Y
REWRITE_PARTIAL_TEXT_MATCH Y
REWRITE_GENERAL Y
REWRITE_PCT N general rewrite is not possible or PCT is not possible on an
PCT_TABLE_REWRITE N A relation is not a partitioned table
PCT_TABLE_REWRITE N B relation is not a partitioned table
同样重要的`REWRITE_GENERAL = 'Y'。
请注意,我使用的是 18.4 XE,这很可疑,应该向 Oracle 支持部门澄清。
最后的好消息是,如果您使用 Oracle join 定义 MV
,您可以使用 ASNI 连接,您将看到重写:
示例
create materialized view MV2
enable query rewrite
as
select
A.y,B where A.x = B.x
EXPLAIN PLAN SET STATEMENT_ID = 'jara1' into plan_table FOR
select A.y,B.z from A join B on A.x = B.x where y = 'A' and z = 'Z'
;
SELECT * FROM table(DBMS_XPLAN.DISPLAY('plan_table','jara1','ALL'));
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3846 | 15384 | 456 (8)| 00:00:01 |
|* 1 | MAT_VIEW REWRITE ACCESS FULL| MV2 | 3846 | 15384 | 456 (8)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("MV2"."Z"='Z' AND "MV2"."Y"='A')