跳至主要内容
版本:v3

Incremental Materialized View

什麼是 Incremental Materialized View ?

我們在 產生 Materialized View (資料實體化) 章節中了解到如何創建 Materialized View,但 Materialized View 在更新時,整個 Query 會重新執行,資料也會從來源重新抓取,若資料量大,會造成系統較大的負擔.

而 Incremental Materialized View (增量式資料實體化) 就是為了解決這個問題而設計出來的,運作機制如下:

  1. 使用者需指定來源的 Table,Incremental Key 以及 Unique Key

    1. Incremental Key (Required): 每次更新會記錄此欄位的最大值,並對資料來源下大於 (>) 此最大值的 Query,取得片段的更新資料.
    2. Unique Key (Optional): 在後續增量式更新時,會以此欄位做資料合併,若不指定此欄位,將會忽略來自資料來源 update row 的更新,並只把 insert 到資料來源的新資料 append 到 Materialized View 中.
  2. 在第一次抓取時,如同一般的 Materialized View,整份資料會被複製進來,這時我們會紀錄 incremental key 此欄位的最大值. flow-1

    我們抓取 users 這張表,把 id 當作 unique key,updated_at 當作 incremental key.

  3. 第二次之後更新,就會是增量式的更新, flow-2

    上一次更新,我們紀錄 update_at 的最大值是 1/5,我們從來源端取得 > 1/5 的資料,並使用 id 合併更新舊資料,若原本沒有此 id 對應的資料,就會 insert 成新資料.

注意

目前 Canner Enterprise 僅支援資料有 Append 及 Update 的更新,尚未支援 Delete 情況的更新

操作步驟

Step 1: 準備 Tables / Views

您需要在工作區中準備好做 Incremental Materialized View 的 Tables 或 Views。

在 Datasets 右上方的篩選器中,選取 Tables 或 Views 類型以建立 Incremental Materialized View。

1_incremental_mview

篩選完後,點擊表格中的 ... 圖示展開選單,選取 Create an Incremental Materialized View 選項。

2_incremental_mview

Step 2: 填寫 Incremental Materialized View 的資訊

  • Name: Materialized View 的名稱
  • Select a source table: 從下拉選擇來源的 Table
  • Columns: 選擇需要的欄位
  • Select unique key(s): 選擇 unique key 的欄位,在後續增量式更新時,會以此欄位做資料合併,若不指定此欄位,將會忽略來自資料來源 update row 的更新,並只把 insert 到資料來源的新資料 append 到 Materialized View 中.
  • Select incremental key: 選擇 incremental key 的欄位,每次更新會記錄此欄位的最大值,並對資料來源下大於 (>) 此最大值的 Query,取得片段的更新資料,僅接受 TINYINTSMALLINTINTEGERBIGINTREALDOUBLEDECIMALDATETIMETIMESTAMP 等型態.
  • Schedule: 可以針對您的需求每小時、每天、每月進行排程,或客製化時程。定期更新,可設定間隔小時 (Hourly),每天時間 (Daily),每週時間 (Weekly),或自定義 Crontab 語法。若在 Crontab 中指定特定的 Hour,需注意這邊的時間是 UTC +0 時區。

3_incremental_mview 4_incremental_mview

Step 3: 建立完成

當 Incremental Materialized View 建立完成後,將右上的篩選器設定完篩選 Materialized View 類型 Dataset。

5_incremental_mview

篩選完後,即可在畫面中看到剛建立完成的 Incremental Materialized View,點擊此 dataset 的名字時可進入細節頁面。

6_incremental_mview

在細節頁面,名稱標題下方可以看到其細節資訊。

7_incremental_mview