Oracle Materialized View refresh


簡述

Materialized views - 物化視圖 - 簡稱 MV,是已經被存儲的或者說被物化-'materialized' 成 schema對象的查詢結果。其中查詢的 'From' 子句可以給 table, view 和 materialized view命名。這些用來建立 MV的對象都可以被稱為 主表-master tables (a replication term) 或者 具體表-detail tables (a data warehousing term)。

Refresh Process

MV中的數據不必和主表-master table 當前的數據總是一致。為了保持 MV中的數據和主表數據一致,必須來刷新 MV。

對於刷新 MV, Oracle 支持幾種刷新類型。

Refresh Types

Oracle 支持快速刷新 - fast, 完全刷新 - complete 和 強制刷新 - force。

完全刷新 - Complete Refresh

DBMS_MVIEW.REFRESH(MV_NAME, method =>'C');

對 MV執行 complete refresh, server 將會執行 MV的定義語句,本質上就是重新創建一遍這個MV。用新的結果集替換已經存在的舊的結果集。Oracle 可以對任意一個 MV 執行 complete refresh。Complete refresh 執行的時間依賴於其定義語句的查詢效率,基本上來說,complete refresh 相比於快速刷新-fast refresh 時間長。

如果對於主 MV執行了 complete refresh,那么依賴於這個 MV創建的其他 MV也必須使用 complete refresh。如果對這些 MV使用 fast refresh,Oracle 會返回 error:

  ORA-12034 mview log is younger than last refresh

快速刷新 - Fast Refresh

DBMS_MVIEW.REFRESH(MV_NAME, method =>'F');

執行 fast refresh, Oracle會首先識別當前主表和最后一次刷新 MV時候數據的變化,根據變化的數據更新 MV。當變化比較小的時候,Fast Refresh 比 Complete Refresh更加高效,因為數據量比較少。

因為 fast refresh 時需要和最近一次更新后的數據進行比較,所以需要 MV所依賴的主表必須有日志log 文件。並且,為了 fast refresh 比 complete refresh 更快速,在創建 MV時每個join 的 column 必須有索引。

另外,如果 MV是基於 partitioned master tables,則可能需要使用 PCT Refresh - Partition Change Tracking。

DBMS_MVIEW.REFRESH(MV_NAME, 'P');

強制刷新 - Force Refresh

DBMS_MVIEW.REFRESH(MV_NAME, method =>'?');

執行 force refresh 時, Oracle會先試圖執行 fast refresh。如果發現 fast refresh 不可行,則執行 complete refresh。

刷新方法

Refresh Option Parameter Description
COMPLETE C Refreshes by recalculating the defining query of the materialized view.
FAST F

Refreshes by incrementally applying changes to the materialized view.

For local materialized views, it chooses the refresh method which is estimated by optimizer to be most efficient. The refresh methods considered are log-based FAST and FAST_PCT.

FAST_PCT P Refreshes by recomputing the rows in the materialized view affected by changed partitions in the detail tables.
FORCE ?

Attempts a fast refresh. If that is not possible, it does a complete refresh.

For local materialized views, it chooses the refresh method which is estimated by optimizer to be most efficient. The refresh methods considered are log based FASTFAST_PCT, and COMPLETE.

小結

對於 FAST_PCT 刷新方式沒有使用過,不了解,只是在 Oracle文檔中看到的。MV還可以選擇是 ON COMMIT刷新和ON DEMAND,即主表 commit之后 MV開始刷新和在需要刷新的時候進行刷新的兩種方式。

參考資料

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM