MView“启用查询重写”用法
CREATE TABLE TEST_DATE(COL1 VARCHAR2(20),COL2 NUMBER,COL3_DATE DATE,COL4_DATE DATE)
/
create materialized view TEST_SYS
REFRESH FORCE ON DEMAND
ENABLE QUERY REWRITE --- ????
AS
SELECT COL1,COL2
FROM TEST_date
WHERE TRUNC(SYSDATE) BETWEEN TRUNC(COL3_DATE) AND TRUNC(COL4_DATE)
/
如果禁用“启用查询重写”选项,则会为上述查询创建 MView,那么在创建物化视图时使用 ENABLE QUERY REWRITE 子句的目的是什么?我们可以删除它并创建它吗?我们是否必须在性能上做出妥协MView 如果我们必须注释 Enable Query Rewrite。
请详细解释一下启用查询重写选项的使用。
CREATE TABLE TEST_DATE(COL1 VARCHAR2(20),COL2 NUMBER,COL3_DATE DATE,COL4_DATE DATE)
/
create materialized view TEST_SYS
REFRESH FORCE ON DEMAND
ENABLE QUERY REWRITE --- ????
AS
SELECT COL1,COL2
FROM TEST_date
WHERE TRUNC(SYSDATE) BETWEEN TRUNC(COL3_DATE) AND TRUNC(COL4_DATE)
/
If Enable Query Rewrite option is disabled, MView is getting created for the above query then what is the purpose of having ENABLE QUERY REWRITE clause while creating materialized view, can we remove it and create it, and do we have to compromise on the performance of the MView if we have to comment Enable Query Rewrite.
Please explain me the use of enable query rewrite option in detail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询重写允许 Oracle 重写针对基表(在本例中为 TEST_DATE)的查询,以透明地使用物化视图(在本例中为 TEST_SYS)。例如,当您的物化视图预聚合数据时,这非常有用。如果我有一个事务表和一个物化视图
,那么 Oracle 可以转换查询,例如
使用物化视图而不是访问基表。如果您创建了适当的维度对象,则可以将这样的查询
重写为使用物化视图而不是基表。
如果未启用查询重写,则只有在显式查询物化视图而不是查询基表时,您才会看到使用物化视图带来的性能优势。这通常需要更多的开发工作,并限制了 DBA 未来通过微调物化视图来调整应用程序的能力。
在您的情况下,WHERE 子句中 SYSDATE 的存在将阻止查询重写,因为 Oracle 无法确定针对 TEST_DATE 的查询实际上能够使用物化视图。据 Oracle 所知,物化视图刷新时满足条件的物化视图中的数据不再满足条件,而未进入物化视图的数据现在满足条件,仅仅是因为 SYSDATE 已更改。
Query rewrite allows Oracle to rewrite a query against the base table (in this case TEST_DATE) to use the materialized view (in this case TEST_SYS) transparently. That is highly useful when your materialized view is pre-aggregating data, for example. If I have a transactions table and a materialized view
then Oracle could transform a query like
to use the materialized view rather than hitting the base table. And if you have appropriate dimension objects created, you could have a query like
that could also be rewritten to use the materialized view rather than the base table.
If query rewrite is not enabled, you would only see a performance benefit from using the materialized view if you explicitly queried the materialized view rather than querying the base table. That generally requires more development effort and limits the ability of the DBAs to tune the application in the future by fine-tuning materialized views.
In your case, the presence of SYSDATE in your WHERE clause is going to prevent query rewrite because Oracle wouldn't be able to figure out that a query against TEST_DATE would actually be able to use the materialized view. For all Oracle knows, data in the materialized view that satisfied the condition when the materialized view was refreshed no longer satisfies the condition and data that didn't make it into the materialized view now satisfies the condition simply because the SYSDATE has changed.
您不需要启用查询重写。此外,考虑到您的 mview 定义,它可能不会对您有任何帮助。
You do not need to have query rewrite enabled. Further, given your mview definition, it probably wouldn't hep you anyway.