Oracle物化视图2 -- Query Rewrite及参数

Query Rewrite的条件

  • Individual materialized views must have the ENABLE QUERY REWRITE clause.

  • The session parameter QUERY_REWRITE_ENABLED must be set to TRUE (the default) or FORCE.

  • Cost-based optimization must be used by setting the initialization parameter OPTIMIZER_MODE to ALL_ROWS, FIRST_ROWS, or FIRST_ROWS_n.

影响Query Rewrite的参数

  • QUERY_REWRITE_ENABLED = TRUE (default), FALSE, or FORCE

  • QUERY_REWRITE_INTEGRITY - STALE_TOLERATED, TRUSTED, or ENFORCED (the default)

  • OPTIMIZER_MODE = ALL_ROWS (default), FIRST_ROWS, or FIRST_ROWS_n

详细介绍Query_Rewrite_Integrity参数

Modifiable - ALTER SESSION, ALTER SYSTEM

  • Enforced - This is the default mode. The optimizer only uses fresh data from the materialized views and only use those relationships that are based on ENABLED VALIDATED primary, unique, or foreign key constraints.

Query the user_mview to view the staleness of given materialized view.

  • Trusted - In TRUSTED mode, the optimizer trusts that the relationships declared in dimensions and RELY constraints are correct. In this mode, the optimizer also uses prebuilt materialized views or materialized views based on views, and it uses relationships that are not enforced as well as those that are enforced. In this mode, the optimizer also trusts declared but not ENABLED VALIDATED primary or unique key constraints and data relationships specified using dimensions. This mode offers greater query rewrite capabilities but also creates the risk of incorrect results if any of the trusted relationships you have declared are incorrect.

也就是所,Oracle信任用户声明的数据完整性,MV中数据新旧程度,dimension中定义的关系。同时,当使用prebuilt materialized view时,需要使用这个设置。

  • Stale_tolerated - In STALE_TOLERATED mode, the optimizer uses materialized views that are valid but contain stale data as well as those that contain fresh data. This mode offers the maximum rewrite capability but creates the risk of generating inaccurate results.

Trusted Query_Rewrite_Integrity实例

回到上一篇中遗留的问题。

SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, R_OWNER, R_CONSTRAINT_NAME, STATUS, VALIDATED, RELY
FROM USER_CONSTRAINTS
WHERE table_name='SALES';

145220688.jpg

可以看到外键关联都是not validated.

SELECT mview_name, build_mode,staleness,unknown_prebuilt
FROM user_mviews WHERE MVIEW_NAME='CAL_MONTH_SALES_MV';


153313671.jpg这是一个Prebuilt MV,Query_Rewrite_Integrity=Enforced时,Oracle是不会使用MV来重写该条SQL的。

Alter session set Query_Rewrite_Integrity=trusted;

重新explain plan上一篇中的select语句,可以看到MV已经被使用了。

NonValidated Rely Constraint与Query_Rewrite_Integrity=Enforced

对于普通物化视图(非Prebuilt),当Query_Rewrite_Integiry=Enforeced时,nonvalidated rely外键依然支持Query Rewrite。下面给出一个例子:

CREATE MATERIALIZED VIEW MONTHLY_SALES_MV
  BUILD IMMEDIATE
  REFRESH COMPLETE
  ENABLE QUERY REWRITE
AS
  SELECT t.calendar_month_desc, SUM(s.amount_sold) AS dollars , COUNT(s.quantity_sold) AS quantity_sold
  FROM sales s , times t
  WHERE s.time_id = t.time_id
  GROUP BY t.calendar_month_desc;

materialized view MONTHLY_SALES_MV created.

SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, R_OWNER, R_CONSTRAINT_NAME, STATUS, VALIDATED, RELY
FROM USER_CONSTRAINTS
WHERE table_name='SALES';

152103483.jpg
show parameter rewrite;
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled                string      TRUE
query_rewrite_integrity              string      enforced

我们来执行一条物化视图中的select语句,查看其执行计划:
152319502.jpg

本文出自 “981428” 博客,转载请与作者联系!

你可能感兴趣的:(参数,query,rewrite,Oracle物化视图2)