Centipede Fact Table (ムカデファクトテーブル) は、[[ファクトテーブル]]の FK 数が膨大になり、図示すると脚が異常に多いムカデのように見える [[ディメンショナルモデリング]] のアンチパターンである。ディメンションが多すぎると、データ利用時の認知負荷が膨れ上がる。
[[Ralph Kimball]] は、ほとんどのビジネスプロセスは 20 未満のディメンションで足り、25 以上なら相関するディメンションの統合を検討すべきとしている。別々に切り出したディメンションを最小数に束ねて FK を減らすのが正しい設計である。
> [!info]
> 原典は『[[The Data Warehouse Toolkit]]』(3rd Edition) 3 章 "Retail Sales"。Kimball Group の [Centipede Fact Table](https://www.kimballgroup.com/data-warehouse-business-intelligence-resources/kimball-techniques/dimensional-modeling-techniques/centipede-fact-table/) テクニックとしても解説されている。
> [!note]
> 行指向 DB の時代は FK 数がストレージ・インデックス・クエリ性能に直結したが、列指向 DB が普及したことによりパフォーマンス面は大幅に改善されている。ただし、ディメンション横断のブラウザビリティは依然損なわれる。
## 構造
次の ER 図は、注文ファクトテーブル `fct_order` に、日付の階層レベルと低カーディナリティ属性が別々のディメンションとして作成されている例である。
```mermaid
erDiagram
dim_date ||--|{ fct_order : "date_key"
dim_week ||--|{ fct_order : "week_key"
dim_month ||--|{ fct_order : "month_key"
dim_quarter ||--|{ fct_order : "quarter_key"
dim_year ||--|{ fct_order : "year_key"
fct_order ||--|| dim_order_status : "order_status_key"
fct_order ||--|| dim_payment_method : "payment_method_key"
fct_order ||--|| dim_promotion_type : "promotion_type_key"
fct_order {
order_key int64 "[SK]"
date_key int64 "[FK]"
week_key int64 "[FK]"
month_key int64 "[FK]"
quarter_key int64 "[FK]"
year_key int64 "[FK]"
order_status_key int64 "[FK]"
payment_method_key int64 "[FK]"
promotion_type_key int64 "[FK]"
order_id string "[GD, DD]"
order_date datetime "[DD]"
item_quantity int64 "[FA]"
order_amount int64 "[FA]"
}
```
このように FK が何本も伸びて脚の多いムカデ(Centipede)に見えることが、Centipede Fact Table という名前の由来である。
## 原因
### 1. 階層属性をファクトテーブルに直接結合する
OLTP 出身の設計者は、第三正規形でデータを管理する習慣が染み付いていることが多く、ディメンションを非正規化して冗長に持つことに抵抗を抱きやすい。このような設計者は、スノーフレークスキーマが非推奨だと学習しても、正規化への衝動が抑えきれない。
その結果、ディメンション側ではなくファクト側に各階層レベルを直接結合することがある。日付なら週・月・四半期・年が別々のディメンションになり、FK 数が増えてしまう。
### 2. 低カーディナリティ属性ごとにディメンションを作成する
ファクトイベントに紐づく支払方法・注文ステータス・プロモーション種別のような低カーディナリティ属性は、互いに無関係なので別々のディメンションにしたくなる。しかし属性ごとにディメンションを切り出すと、その数だけ FK が増えてしまう。
## 解決策
次の 2 つのルールを遵守すると、`fct_order` から伸びる FK は `date_key` と `order_profile_key` の 2 本に収まる。
1. 互いに相関する階層属性は、最小粒度のディメンション 1 つにまとめる
2. 低カーディナリティ属性は、 [[Junk Dimension]] にまとめる
```mermaid
erDiagram
dim_date ||--|{ fct_order : "date_key"
dim_order_profile ||--|{ fct_order : "order_profile_key"
fct_order {
order_key int64 "[SK]"
date_key int64 "[FK]"
order_profile_key int64 "[FK]"
order_id string "[GD, DD]"
order_date datetime "[DD]"
item_quantity int64 "[FA]"
order_amount int64 "[FA]"
}
dim_order_profile {
order_profile_key int64 "[SK]"
order_status string "[GD]"
payment_method string "[GD]"
promotion_type string "[GD]"
}
dim_date {
date_key int64 "[SK]"
full_date date "[GD]"
week int64 "[GD]"
month int64 "[GD]"
quarter int64 "[GD]"
year int64 "[GD]"
}
```
## 関連ノート
- **[派生]** [[長文テキスト属性は Text Comment Dimension に切り出す]]: 長文テキスト属性が複数あるとファクトから多数の FK が伸びる現象が、Centipede と類似の構造で生じる