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 | 2013/02/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=2の行のorderDate の値が更新され、新しい値がcheckExpressionValue と一致します。
4. インクリメンタル最適化ジョブの実行 (newRowCheckExpression = "orderDate", identityExpression = "'id'", deleteOldDate= "true")
4.1 Materialized Table にすでにあるデータの検索
すでにdwh.table に存在する最後の行を推定するために、インクリメンタルジョブは、最初に以下を発行し
SELECT MAX(orderDate) as checkExprValue FROM dwh.tableそして以下を取得します
checkExprValue = '2012/01/01'4.2 DELTA テーブルの作成と入力
DELTA は以下のように計算されます:
SELECT * INTO dwh.delta FROM source.table WHERE orderDate >= '2012/01/01'dwh.delta
id | orderDate | description |
|---|---|---|
2 | 2013/02/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 |
4.3.b 元の Materialized Tableから更新する行を削除します (identityExpression 値を使用)。
DELETE FROM dwh.table WHERE id IN (SELECT id FROM dwh.delta)その後、Materialized Tableには以下の行が含まれます:
dwh.table
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
Please, note that the identity expression is provided as a SQL code and it will be injected in the DELETE statement as follows:
DELETE FROM dwh.table WHERE <identityExpression> IN (SELECT <identityExpression> FROM dwh.delta)If the identity expression should contain more than one column, it can be done by concatenating them to a string
CONCAT ( CAST ( id as string ), STRINGCOL1 )Please pay attention to substituting null values and data types which cannot be implicitly cast to string have to be cast explicitly: xml, clob. Data types which cannot be cast to a string cannot be used in identity expression: blob, object.
4.4 元のマテリアライズドテーブルにDELTA を追加します。
次のように計算され
INSERT INTO dwh.table SELECT * FROM dwh.delta以下の結果になります
dwh.table
id | orderDate | description |
|---|---|---|
1 | 2010/01/01 | descr_1 |
2 | 2013/02/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 |
It is important to note that using the identityExpression prevented the generation of duplicates. If the identityExpression were NULL, two rows with id = 2 would have been copied into the materialized table.