次の例を考えてみましょう:
id (主キー) と orderDate (チェック式として使用) の列を持つソース・テーブルがあります。 この最初の例では、identityExpression、NULLです。
1. 最初の状況
source.table Materialization前
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2011/01/01 | descr_2 |
3 | 2012/01/01 | descr_3 |
2. 最適化ジョブの実行(フルコピー):
source.table は、まずdwh.table に完全にコピーされます:
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2011/01/01 | descr_2 |
3 | 2012/01/01 | descr_3 |
3. Source テーブルが成長し、いくつかの行が更新されます(青色でハイライトされます)
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2011/01/01 | descr_2_changed |
3 | 2012/01/01 | descr_3_changed |
3.1 | 2012/01/01 | descr_3.1 |
4 | 2012/06/01 | descr_4 |
5 | 2013/02/01 | descr_5 |
(idが3と3.1の行はorderDateが同じであることに注意してください)
4. インクリメンタル最適化ジョブの実行 (newRowCheckExpression = "orderDate")
4.1 Materialized Table にすでにあるデータの検索
すでにdwh.table に存在する最後の行を推定するために、インクリメンタルジョブは、最初に以下を発行し
SELECT MAX(orderDate) as checkExprValue FROM dwh.tableそして以下を取得します
checkExprValue = '2012/01/01'4.2 DELTA テーブル の作成と入力
DELTAは以下のように計算されます:
4.2.1 deleteOldData = false
SELECT * INTO dwh.delta FROM source.table WHERE orderDate > '2012/01/01'4.2.2 deleteOldData = true
SELECT * INTO dwh.delta FROM source.table WHERE orderDate >= '2012/01/01'dwh.delta (deleteOldData = false)
id | orderDate | description |
|---|---|---|
4 | 2012/06/01 | descr_4 |
5 | 2013/02/01 | descr_5 |
dwh.delta (deleteOldData = true)
id | orderDate | description |
|---|---|---|
3 | 2012/01/01 | descr_3_changed |
3.1 | 2012/01/01 | descr_3.1 |
4 | 2012/06/01 | descr_4 |
5 | 2013/02/01 | descr_5 |
4.3 元のマテリアライズドテーブルから更新する行を削除します。
このステップは、deleteOldDataをtrueに設定してインクリメンタル更新を実行した場合にのみ実行されます。
DELETE FROM dwh.table WHERE orderDate >= '2012/01/01'その後、Materialized Tableには以下の行が含まれます:
dwh.table (deleteOldData = true)
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2011/01/01 | descr_2 |
4.4 元のマテリアライズドテーブルにDELTA を追加します。
次のように計算され
INSERT INTO dwh.table SELECT * FROM dwh.delta以下の結果になります
dwh.table (deleteOldData = false)
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2011/01/01 | descr_2 |
3 | 2012/01/01 | descr_3 |
4 | 2012/06/01 | descr_4 |
5 | 2013/02/01 | descr_5 |
dwh.table (deleteOldData = true)
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2011/01/01 | descr_2 |
3 | 2012/01/01 | descr_3_changed |
3.1 | 2012/01/01 | descr_3.1 |
4 | 2012/06/01 | descr_4 |
5 | 2013/02/01 | descr_5 |
It is important to note how the row with id = 2 was not updated (its description value is "descr_2" instead of "descr_2_changed") since this row does not match the criteria provided by the newRowCheckExpression parameter.