マージの例 - Amazon Redshift

マージの例

以下の例では、マージを実行して SALES テーブルを更新します。最初の例では、ターゲットテーブルから削除した後でステージングテーブルからすべての行を挿入する、よりシンプルな方法を使用しています。2 番めの例では、ターゲットテーブル内の選択した列の更新が必要であるため、追加の更新ステップが含まれています。

マージの例 は、Amazon Redshift 用のサンプルデータセット (TICKIT データセット) を使用します。前提条件として、「一般的なデータベースタスクの開始方法」ガイドの手順に従って、TICKIT テーブルとデータを設定できます。サンプルデータセットの詳細については、「サンプルデータベース」を参照してください。

マージデータソースのサンプル

このセクションの例では、更新と挿入を両方とも含んでいるサンプル データソースが必要です。これらの例では、SALES テーブルからのデータを使用する SALES_UPDATE というサンプルテーブルを作成します。12 月の新しい販売アクティビティを表すランダムデータを新しいテーブルに入力します。SALES_UPDATE サンプルテーブルを使用して、ステージングテーブルを以下の例で作成します。

-- Create a sample table as a copy of the SALES table create table sales_update as select * from sales; -- Change every fifth row so we have updates update sales_update set qtysold = qtysold*2, pricepaid = pricepaid*0.8, commission = commission*1.1 where saletime > '2008-11-30' and mod(sellerid, 5) = 0; -- Add some new rows so we have insert examples -- This example creates a duplicate of every fourth row insert into sales_update select (salesid + 172456) as salesid, listid, sellerid, buyerid, eventid, dateid, qtysold, pricepaid, commission, getdate() as saletime from sales_update where saletime > '2008-11-30' and mod(sellerid, 4) = 0;

既存の行を置き換えるマージの例

以下のスクリプトでは、SALES_UPDATE テーブルを使用して、12 月の販売アクティビティの新しいデータによる SALES テーブルでのマージ操作を実行します。この例では、更新を含む SALES テーブルの行を削除して、ステージングテーブル内の更新された行で置き換えることができるようにします。ステージングテーブルにはマージに関与する行のみが含まれるため、CREATE TABLE ステートメントには変更されていない行を除外するフィルタが含まれます。

-- Create a staging table and populate it with updated rows from SALES_UPDATE create temp table stagesales as select * from sales_update where sales_update.saletime > '2008-11-30' and sales_update.salesid = (select sales.salesid from sales where sales.salesid = sales_update.salesid and sales.listid = sales_update.listid and (sales_update.qtysold != sales.qtysold or sales_update.pricepaid != sales.pricepaid)); -- Start a new transaction begin transaction; -- Delete any rows from SALES that exist in STAGESALES, because they are updates -- The join includes a redundant predicate to collocate on the distribution key –- A filter on saletime enables a range-restricted scan on SALES delete from sales using stagesales where sales.salesid = stagesales.salesid and sales.listid = stagesales.listid and sales.saletime > '2008-11-30'; -- Insert all the rows from the staging table into the target table insert into sales select * from stagesales; -- End transaction and commit end transaction; -- Drop the staging table drop table stagesales;

列リストを指定するマージの例

以下の例では、12 月の販売アクティビティの新しいデータで SALES を更新するマージ操作を実行します。更新と挿入の両方を含み、変更されていない行を含む、サンプルデータが必要です。この例では、QTYSOLD および PRICEPAID 列を更新し、COMMISSION および SALETIME は変更しません。以下のスクリプトでは、SALES_UPDATE テーブルを使用して、SALES テーブルでのマージ操作を実行しています。

-- Create a staging table and populate it with rows from SALES_UPDATE for Dec create temp table stagesales as select * from sales_update where saletime > '2008-11-30'; -- Start a new transaction begin transaction; -- Update the target table using an inner join with the staging table -- The join includes a redundant predicate to collocate on the distribution key –- A filter on saletime enables a range-restricted scan on SALES update sales set qtysold = stagesales.qtysold, pricepaid = stagesales.pricepaid from stagesales where sales.salesid = stagesales.salesid and sales.listid = stagesales.listid and stagesales.saletime > '2008-11-30' and (sales.qtysold != stagesales.qtysold or sales.pricepaid != stagesales.pricepaid); -- Delete matching rows from the staging table -- using an inner join with the target table delete from stagesales using sales where sales.salesid = stagesales.salesid and sales.listid = stagesales.listid; -- Insert the remaining rows from the staging table into the target table insert into sales select * from stagesales; -- End transaction and commit end transaction; -- Drop the staging table drop table stagesales;