Core and Custom Dimension は、商品のように同じ種類の対象を表しながら、販売チャネルなどの種別ごとに固有の属性を持つ [[ディメンションテーブル]] を、共通属性だけの Core Dimension と、固有属性を加えた Custom Dimension に分ける設計パターンである。 この手法は [[ディメンショナルモデリング]] の設計テクニックの 1 つである。Core Dimension と Custom Dimension は同じサロゲートキーを共有し、どちらも同じ [[ファクトテーブル]] に結合できる。 > [!info] > 原典は『[[The Data Warehouse Toolkit]]』(3rd Edition) 10 章 "Financial Services" の "Supertype and Subtype Schemas for Heterogeneous Products" 節。 > > Kimball Group の [Supertype and Subtype Schemas for Heterogeneous Products](https://www.kimballgroup.com/data-warehouse-business-intelligence-resources/kimball-techniques/dimensional-modeling-techniques/supertype-subtype-heterogeneous/) テクニックとして紹介されている。 > [!note] > [[Ralph Kimball]] はこの構成を supertype / subtype と呼ぶ。Adamson の『[[Star Schema The Complete Reference]]』は core / custom と呼び、13 章 "Type-Specific Stars" で体系的に解説している。 > > 本サイトでは Core and Custom Dimension で統一する。ER モデリングの subtype は主キーと固有属性だけを持つ表を指すことが多く、本パターンの構造と取り違えやすいためである。 ## 分ける理由 固有属性をすべて 1 つのディメンションテーブルに載せると、ほかの種別の行では意味を持たない列が、種別が増えるたびに増えていく。Kimball は銀行の商品を例に、こうした列が数百に達し、どの行でもそのほとんどが空になると述べている。 次の図と表のように、Shopify の行は `amazon_` で始まる列が、Amazon の行は `shopify_` で始まる列がすべてデフォルト値 (NULL の代わりに入れるラベル) になる。表の 1 行は、販売チャネルに出品した商品 1 件である。 ```mermaid erDiagram dim_product ||--|{ fct_order : product_key fct_order["fct_order [TF]"] { order_key int64 "[SK]" product_key int64 "[FK]" order_id string "[GD, DD]" quantity int64 "[FA]" order_amount int64 "[FA]" } dim_product["dim_product [CV]"] { product_key int64 "[SK]" product_id string "[GD]" platform string "[GD]" product_name string brand string shopify_handle string shopify_vendor string shopify_tags string amazon_asin string amazon_fulfillment_channel string amazon_browse_node string } ``` ``` dim_product | product_key (SK) | product_id (NK) | platform (NK) | product_name | ... | shopify_handle | ... | amazon_asin | ... | | ---------------- | --------------- | ------------- | ------------ | --- | ---------------- | --- | ---------------- | --- | | 1 | shp-001 | shopify | Linen Shirt | ... | linen-shirt | ... | (Not Applicable) | ... | | 2 | shp-002 | shopify | Canvas Tote | ... | canvas-tote | ... | (Not Applicable) | ... | | 3 | amz-001 | amazon | Linen Shirt | ... | (Not Applicable) | ... | B0EXAMPLE1 | ... | ``` > [!note] > `(Not Applicable)` は、その行に当てはまらない属性を NULL のままにせず入れておくデフォルト値である。ラベルの語彙は [[Default Row]] の属性に入れるものと揃えている。 Kimball によれば、業務の分析のほとんどは、種別を横断するか 1 つの種別を掘り下げるかのどちらかである。複数の種別の固有属性を同時に使う場面は少ない。そこで、横断する分析には Core Dimension を、掘り下げる分析には Custom Dimension を用意する。 ## 構造 Core Dimension と Custom Dimension は、持つ行と列が異なる。 - **Core Dimension**: 全種別の行、共通属性の列 - **Custom Dimension**: 1 つの種別の行、共通属性 + 固有属性の列 Custom Dimension が共通属性も持つのは、1 つの種別を分析するときに Core Dimension との結合を不要にするためである。キーが共通なので、分析に応じてファクトテーブルの結合先を差し替えられる。 前節の `dim_product` を分けると、Core Dimension の `dim_product` と、Custom Dimension の `dim_product_shopify` / `dim_product_amazon` になる。 ```mermaid erDiagram dim_product ||--|{ fct_order : product_key fct_order }|--o| dim_product_shopify : product_key fct_order }|--o| dim_product_amazon : product_key fct_order["fct_order [TF]"] { order_key int64 "[SK]" product_key int64 "[FK]" order_id string "[GD, DD]" quantity int64 "[FA]" order_amount int64 "[FA]" } dim_product["dim_product [CV]"] { product_key int64 "[SK]" product_id string "[GD]" platform string "[GD]" product_name string brand string } dim_product_shopify["dim_product_shopify [CV]"] { product_key int64 "[SK]" product_id string "[GD]" platform string "[GD]" product_name string brand string handle string vendor string tags string } dim_product_amazon["dim_product_amazon [CV]"] { product_key int64 "[SK]" product_id string "[GD]" platform string "[GD]" product_name string brand string asin string fulfillment_channel string browse_node string } ``` ### 行の対応 同じ商品は、Core Dimension と Custom Dimension で同じ `product_key` と同じ共通属性の値を持つ。次の例では、`product_key` が 1 と 2 の行が `dim_product_shopify` に、3 の行が `dim_product_amazon` に入る。 ``` dim_product | product_key (SK) | product_id (NK) | platform (NK) | product_name | brand | | ---------------- | --------------- | ------------- | ------------ | ----- | | 1 | shp-001 | shopify | Linen Shirt | Acme | | 2 | shp-002 | shopify | Canvas Tote | Acme | | 3 | amz-001 | amazon | Linen Shirt | Acme | dim_product_shopify | product_key (SK) | product_id (NK) | platform (NK) | product_name | brand | handle | vendor | tags | | ---------------- | --------------- | ------------- | ------------ | ----- | ----------- | ------------ | --------------- | | 1 | shp-001 | shopify | Linen Shirt | Acme | linen-shirt | Acme Apparel | apparel, summer | | 2 | shp-002 | shopify | Canvas Tote | Acme | canvas-tote | Acme Goods | bag, canvas | dim_product_amazon | product_key (SK) | product_id (NK) | platform (NK) | product_name | brand | asin | fulfillment_channel | browse_node | | ---------------- | --------------- | ------------- | ------------ | ----- | ---------- | ------------------- | ----------- | | 3 | amz-001 | amazon | Linen Shirt | Acme | B0EXAMPLE1 | FBA | Shirts | ``` > [!warning] > Custom Dimension を内部結合すると、WHERE 句がなくてもファクトテーブルはその種別の行に絞られる。Custom Dimension の列をクエリから外して結合が消えると、絞り込みも消えて全種別の集計に戻る。 > > Adamson は対策として、その種別の行だけを持つファクトテーブルをビューで用意することを勧めている。 ## 命名規約 ### テーブル名 Core Dimension は、分割する前の名前 `dim_{{ ENTITY }}` をそのまま使う。Custom Dimension は、その名前に種別名を足した `dim_{{ ENTITY }}_{{ TYPE }}` の形に揃える。 この形にすると、テーブル名から Core Dimension との対応と種別の両方が読み取れる。 - ✅ 推奨: `dim_product` と `dim_product_shopify` (名前の先頭が揃い、種別も読み取れる) - ❌ 非推奨: `dim_shopify_product` (テーブル一覧で Core Dimension と離れて並び、対応が読み取りにくい) - ❌ 非推奨: `dim_product_custom` (どの種別の Custom Dimension かが分からない) - ❌ 非推奨: `dim_product_core` (名前を変えると、共通属性だけを使う既存のクエリまで書き換えが要る) ### 列名 Custom Dimension では、固有属性の列名に種別名を付けない。テーブル名が種別を示しているためである。1 つの表に全種別の固有属性を載せる場合は、`shopify_handle` のように種別名を付けて区別する。 この規則に従うと、1 つの表から分割するときに `shopify_handle` が `handle` へ変わる。 ## 関連ノート - **[深掘り]** [[Default Row の行数は NULL の原因をどこまで区別するかで決める]]: 本ノートの表に出てくる `(Not Applicable)` が、値が無い原因の分類のうちどれに当たるかを確かめられる