ITPub博客

首页 > 数据库 > Oracle > Oracle物化视图2 -- Query Rewrite及参数

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

原创 Oracle 作者:chncaesar 时间:2013-12-09 22:44:39 0 删除 编辑

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.
  • Materialized View must be in 'Freesh' state.

物化视图的Staleness

查看User_MViewsStaleness
FRESH - 无需说明。
STALE - 需要执行一次刷新。
UNUSABLE - 需要执行一次完全刷新,exec dbms_mview.refresh('XXX', 'C');
UNKNOWN - 表明这是个Prebuilt MV。也可以用Unknown_Prebuilt列,该列为'Y',表明这是个Prebuilt MV。
UNDEFINED - 表示MV依赖的表在远程数据库上。
NEEDS_COMPILE - 表明物化视图处在Invalid状态。需要运行Alter materialized view xxx compile;来重新编译物化视图。也可以用Compile_state列。

影响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';

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

select mview_name, build_mode,staleness  from USER_MVIEWS;

所有的Prebuilt MV的新旧程度(staleness)都是未知的。

Query_Rewrite_Integrity=Enforced时,Oracle是不会使用MV来重写该条SQL的。

Alter session set Query_Rewrite_Integrity=trusted;

重新explain plan上一篇中的select语句

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.

SALES_TIME_FK R SH TIMES_PK ENABLED NOT VALIDATED RELY
FROM USER_CONSTRAINTS
WHERE table_name='SALES';
 
show parameter rewrite;
NAME                                               TYPE        VALUE                                                                                                
-------------------------------------------------- ----------- ---------------------------------------------------------------------------------------------------- 
query_rewrite_enabled                              string      TRUE                                                                                                 
query_rewrite_integrity                            string      enforced    

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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/638844/viewspace-1062827/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-07-30

  • 博文量
    102
  • 访问量
    900162