MView“启用查询重写”用法

发布于 2024-10-05 04:23:03 字数 484 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太傻旳人生 2024-10-12 04:23:03

查询重写允许 Oracle 重写针对基表(在本例中为 TEST_DATE)的查询,以透明地使用物化视图(在本例中为 TEST_SYS)。例如,当您的物化视图预聚合数据时,这非常有用。如果我有一个事务表和一个物化视图

CREATE MATERIALIZED VIEW mv_transaction_daily
  REFRESH FORCE ON DEMAND
  ENABLE QUERY REWRITE
AS
SELECT store_id,
       transaction_day,
       SUM(transaction_amount) total_transaction_amount
  FROM transactions
 GROUP BY store_id, transaction_day

,那么 Oracle 可以转换查询,例如

SELECT store_id,
       transaction_day,
       SUM(transaction_amount) total_transaction_amount
  FROM transactions
 GROUP BY store_id, transaction_day

使用物化视图而不是访问基表。如果您创建了适当的维度对象,则可以将这样的查询

SELECT store_id,
       trunc(transaction_day,'MM'),
       SUM(transaction_amount) monthly_transaction_amount
  FROM transactions
 GROUP BY store_id, trunc(transaction_day,'MM')

重写为使用物化视图而不是基表。

如果未启用查询重写,则只有在显式查询物化视图而不是查询基表时,您才会看到使用物化视图带来的性能优势。这通常需要更多的开发工作,并限制了 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

CREATE MATERIALIZED VIEW mv_transaction_daily
  REFRESH FORCE ON DEMAND
  ENABLE QUERY REWRITE
AS
SELECT store_id,
       transaction_day,
       SUM(transaction_amount) total_transaction_amount
  FROM transactions
 GROUP BY store_id, transaction_day

then Oracle could transform a query like

SELECT store_id,
       transaction_day,
       SUM(transaction_amount) total_transaction_amount
  FROM transactions
 GROUP BY store_id, transaction_day

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

SELECT store_id,
       trunc(transaction_day,'MM'),
       SUM(transaction_amount) monthly_transaction_amount
  FROM transactions
 GROUP BY store_id, trunc(transaction_day,'MM')

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.

墨离汐 2024-10-12 04:23:03

您不需要启用查询重写。此外,考虑到您的 mview 定义,它可能不会对您有任何帮助。

You do not need to have query rewrite enabled. Further, given your mview definition, it probably wouldn't hep you anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文