LoginSignup
0
0

More than 1 year has passed since last update.

主キーの無いSynapse Analytics 専用SQLプールでデータの重複を防ぐ方法

Posted at

Azure Syanaps Analytics 専用SQLプールでは、以下の記事の通り主キーや一意キーが機能しません。

注意が必要な一意キー、主キーの利用(Azure Synapse Analytics SQLプール)

一方で行の重複を防ぐ必要がある場面は多々発生します。
行の重複を防ぐためにいつも行っている方法を記載します。

値が存在しないことを確認することで行の重複を防ぐ

一般的にDWHシステムでは、テーブルから別のテーブルへデータの移動を行うことが多いと思います。

例えば、以下のような2つのテーブルがあるとします。

stg_table(id,desc_col)
prd_table(id,desc_col)

データがprd_tableにまだ存在しない行のみ、stg_tableからprd_tableにデータ移動を行いたいと思います。
これはよくあるシナリオだと思いますが、ここでよく利用するのはNOT EXISTを利用することです。

insert into prd_table
select
    s.id,s.desc_col
from
    stg_table s
where not exists(
    select 1
    from prd_table p
    where p.id = s.id
);

また、iddesc_colを合わせて一意にしたい場合にはWHERE句を少し拡張します。

insert into prd_table
select
    s.id,s.desc_col
from
    stg_table s
where not exists(
    select 1
    from prd_table p
    where
            p.id = s.id
        and p.desc_col = s.desc_col
);

クラスター化列ストアインデックスで細かくデータをインサートする場合は断片化などでパーフォーマンスが低下する可能性があるので、CTASを使って再作成したり、クラスター化列ストアインデックスの代わりにHEAPテーブルを使うなどの注意が必要な場合があります。

Synapse SQLプールのクラスター化列ストアインデックス

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0