物化视图(materialized view)是一种可以用于汇总(summarize),计算(compute),复制(replicate),及发布(distribute )数据的模式对象(schema object)。
在数据仓库中,物化视图常被用于计算和存储聚合数据(aggregated data),例如汇总(sum),平均(averages)等。在数据仓库环境中,物化视图也被称为概要(summaries),因为 其中通常存储的是汇总数据(summarized data)。用户也可以使用物化视图存储多个表连接后的结果集(compute join)。如果数据库的兼容性参数被设为 Oracle9i 或更高,物化视图的查询中可以包含过滤选择(filter selection)
优化器(optimizer)可以利用物化视图来提升查询性能。优化器能够自动地判断一个存储汇总数据的物化视图是否能满足用户的查询要求,以及使用此物化视图是否能提高查询性能。之后优化器能够重写(rewrite)用户提交的查询以便使用相应的物化视图,而这个重写过程对用户是透明的。此时查询直接使用物化视图,而非用户提交的 SQL 语句中指定的明细数据表或视图。
注意:标准版中不支持查询重写(query rewrite not supported in standard edition)
Example 1: Creating a Materialized View
CREATE MATERIALIZED VIEW LOG ON products WITH SEQUENCE, ROWID
(prod_id, prod_name, prod_desc, prod_subcategory, prod_subcategory_desc,
prod_category, prod_category_desc, prod_weight_class, prod_unit_of_measure,
prod_pack_size, supplier_id, prod_status, prod_list_price, prod_min_price)
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON sales
WITH SEQUENCE, ROWID
(prod_id, cust_id, time_id, channel_id, promo_id, quantity_sold, amount_sold)
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW product_sales_mv
PCTFREE 0 TABLESPACE demo
STORAGE (INITIAL 8k NEXT 8k PCTINCREASE 0)
BUILD IMMEDIATE
REFRESH FAST
ENABLE QUERY REWRITE
AS SELECT p.prod_name, SUM(s.amount_sold) AS dollar_sales,
COUNT(*) AS cnt, COUNT(s.amount_sold) AS cnt_amt
FROM sales s, products p
WHERE s.prod_id = p.prod_id GROUP BY p.prod_name;
物化视图日志(materialized view log)是一种记录物化视图(materialized view)主表(master table)数据修改情况的方案对象(schema object )。使用物化视图日志后,物化视图就可以进行增量刷新。
每个物化视图日志(materialized view log)都与一个主表(master table)相关。物化视图日志与主表位于同一数据库的同一方案(schema)。
注意:上面的例子中使用的刷新方式为fast,是因为在创建物化视图之前就在sales和products表上建立了物化视图日志。下面的例子使用的为完全刷新方式:
Example 2: Creating a Materialized View
CREATE MATERIALIZED VIEW product_sales_mv
PCTFREE 0 TABLESPACE demo
STORAGE (INITIAL 16k NEXT 16k PCTINCREASE 0)
BUILD DEFERRED
REFRESH COMPLETE ON DEMAND
ENABLE QUERY REWRITE AS
SELECT p.prod_name, SUM(s.amount_sold) AS dollar_sales
FROM sales s, products p WHERE s.prod_id = p.prod_id
GROUP BY p.prod_name;
当物化视图(materialized view)的主表(master table)内的数据发生变化后,Oracle 需要进行刷新操作(refreshing)来保证物化视图与主表同步。刷新方式可以是增量的(快速刷新(fast refresh))或完全的。采用快速刷新方式的物化视图,会使用物化视图日志(materialized view log)或直接加载日志(direct loader log)来记录其主表的数据修改情况。
物化视图(materialized view)可以依据用户的请求(on demand)刷新,也可以按照预定的时间间隔刷新。此外,如果物化视图和其主表(master table)处于同一个数据库,那么此物化视图可以在一个修改其主表的事务(transaction)进行提交(commit)的同时被刷新。
下面的例子表明刷新模式是手动还是自动提交(on demand or on commit)
Example 3: Creating a Materialized View
CREATE MATERIALIZED VIEW LOG ON sales WITH SEQUENCE, ROWID
(prod_id, cust_id, time_id, channel_id, promo_id, quantity_sold, amount_sold)
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW sum_sales
PARALLEL
BUILD IMMEDIATE
REFRESH FAST ON COMMIT AS
SELECT s.prod_id, s.time_id, COUNT(*) AS count_grp,
SUM(s.amount_sold) AS sum_dollar_sales,
COUNT(s.amount_sold) AS count_dollar_sales,
SUM(s.quantity_sold) AS sum_quantity_sales,
COUNT(s.quantity_sold) AS count_quantity_sales
FROM sales s
GROUP BY s.prod_id, s.time_id;