DynamoDBのテーブル設計手法
和田哲也
大阪大学工学部電子情報工学科
情報通信工学科目情報システム工学コース
下條研究室
2022年6月1日
2
好きなDBは何ですか?
 私はDynamoDB
3
 初心者
➡ DynamoDBについて知る
 上級者
➡ 今後、このスライドを見ればいつでも
DynamoDBの最適なテーブル設計ができる
このフリートークのゴール
4
 DynamoDBとは
 テーブル設計の難しさ
 テーブル設計手法
 発展編
目次
5
AWSが提供するフルマネージドNoSQLデータベース
 スキーマが柔軟
 新しい列を自由に追加可能
 高速なアクセス
 一秒あたり数千万回のアクセスも可能
 スケーラビリティ
 レコード数やバイト数の制限なし
 耐久性
 自動的に複数のAZにまたがる複数サーバにレプリケート
DynamoDBとは
6
Primary key = Partition key + Sort key
Partition key Sort key value1 value2 value3
aaa 1 taro yes ok
aaa 2 ichiro no ng
bbb 1 hanako yes ok
bbb 2 kano yes ng
ccc 1 yui no ng
ccc 2 kei yes ok
DynamoDBとは
Primary key
Attribute
Table
Item
7
Partition keyごとに物理的にまとめて格納され、
その中でSort keyでソートされている
DynamoDBとは
Partition key=“aaa”
Partition key=“bbb”
Partition key=“ccc”
8
Primary key以外で検索したいならGSI
DynamoDBとは
Partition key Sort key value1 value2 value3
aaa 1 taro yes ok
aaa 2 ichiro no ng
bbb 1 hanako yes ok
bbb 2 kano yes ng
ccc 1 yui no ng
ccc 2 kei yes ok
GSIの
Partition key
に指定
GSIの
Sort keyに
指定
GSI
 GSI(Global Secondary Index)=第二のインデックス
9
テーブルは一つだけにした方がよい
DynamoDBのテーブル設計の難しさ
Primary Key
Attributes
Partition
Key
Sort
Key
PK SK_G... GSI1SK... GSI2.. GSI3PK
77f... post... Post_... 和田
データ
ベース
conte
nt
updated_
at title
この
記...
NoSQ
L...
81f... 77f... Comm...
conte
nt
updated_
at
Com
…
面白
い
山田
太郎
6b9... 77fc.. Like_2...
like_a
uthor
山田
太郎
10
テーブルは一つだけにした方がよい
理由
 料金が安くなる
複数テーブルに分割した場合、アイドル状態のテーブル
が増えて効率が悪い
 テーブル数に上限がある
アカウント当たり256個→2500個に変更された
DynamoDBのテーブル設計の難しさ
11
複数モデルを一つのテーブルに持たせるテーブル設計
の手順が一般には確立されていない
 公式ドキュメントの解説は説明が少なすぎて理解が難しい
 以前インターン先で20時間ほどかけて調査し整理した
テーブル設計手法を解説します
DynamoDBのテーブル設計の難しさ
12
モデルごとにテーブル設計 ➡ テーブルを統合
1. RDBのエンティティを定義する
2. アクセスパターンを書き出し、クエリを明らかにする
3. 各モデルのPrimary Keyを決める
4. アクセスパターンのうちPrimary Keyで実現できないもの
をGSIとして指定する
5. 複数のモデルを一つのテーブルに統合する
テーブル設計手法:全体の流れ
13
例にQiitaのようなサービスのテーブル設計を考える
以下の機能を持つとする
 投稿一覧が見れる
 投稿を作成できる
 投稿に対してコメントを作成できる
 投稿にいいねができる
 特定のユーザーの投稿一覧が見れる
 投稿にジャンルを設定できる
テーブル設計手法:全体の流れ
14
最終的に以下のようなテーブルを設計する
テーブル設計手法:全体の流れ
Primary Key
Attributes
Partition Key Sort Key
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK GSI2PK GSI3PK
77fcab6b post 2021-12-03T10:06:57.650Z 和田哲也
データベー
ス
content updated_at title
この記事の目的
は...
NoSQLの
テーブ
ル...
81fcab6a
Comment_77fc
ab6b 2021-12-03T10:11:44.137Z
content updated_at
comment
_author
面白いですね 山田太郎
6b9b9319 Like_77fcab6b 2021-12-03T10:12:45.033Z
like_author
山田太郎
15
RDB設計の要領でER図を作る
テーブル設計手法:RDBのエンティティを定義する
16
アクセスパターンを書き出し
 投稿一覧を新しい順で取得する(投稿一覧)
 指定されたidの投稿を一件取得する(投稿の個別ページ)
 指定されたidの投稿に紐づくコメントを古い順で全て取得する(投稿の
コメント表示)
 指定されたidの投稿に紐づくいいねを全て取得する(投稿のいいね数
表示)
 指定されたユーザが作成した投稿一覧を新しい順で取得する(ユーザ
ごとの投稿一覧)
 指定されたジャンルの投稿一覧を新しい順で取得する(ジャンルごとの
投稿一覧)
テーブル設計手法:アクセスパターンを書き出し、クエリを明らかにする
17
投稿一覧を取得できるようにER図を修正
テーブル設計手法:アクセスパターンを書き出し、クエリを明らかにする
追加
常に“post”という
文字列を格納
18
クエリを明らかにする
アクセスパターン Model Partition Key Sort Key
投稿一覧を新しい順で取得する Post type created_at
指定されたidの投稿を一件取得する Post id
指定されたidの投稿に紐づくコメントを
古い順で全て取得する
Comment post_id created_at
指定されたidの投稿に紐づくいいねを全て
取得する
Like post_id created_at
指定されたユーザーが作成した投稿一覧を
新しい順で取得する
Post author created_at
指定されたジャンルの投稿一覧を新しい順で
取得する
Post genre created_at
テーブル設計手法:アクセスパターンを書き出し、クエリを明らかにする
19
 Primary Keyはテーブル内で一意になるようにする
 複数の実現方法がある場合はアクセスパターンに登場するものを優先
テーブル設計手法:各モデルのPrimary Keyを決める
Post
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key
id type title author content genre created_at updated_at
77fcab6b post NoSQLのテーブル... 和田哲也 この記事の目的は...データベース 2021-12-03T10:06:57.650Z
Comment
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key
id post_id author content created_at updated_at
81fcab6a 77fcab6b 山田太郎 面白いですね
2021-12-
03T10:11:44.137Z
Like
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key
id post_id author created_at
6b9b9319 77fcab6b 山田太郎
2021-12-
03T10:12:45.033Z
Primary Keyで実現できたアクセスパターンは
「指定されたidの投稿を一件取得する」のみ
20
テーブル設計手法:アクセスパターンのうちPrimary Keyで実現できないも…
Post
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key, GSI1PK GSI2PK GSI3PK GSI1SK,GSI2SK,GSI3SK
id type title author content genre created_at updated_at
77fcab6b post NoSQLのテーブル... 和田哲也 この記事の目的は...データベース 2021-12-03T10:06:57.650Z
Comment
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
id post_id author content created_at updated_at
81fcab6a 77fcab6b 山田太郎 面白いですね
2021-12-
03T10:11:44.137Z
Like
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
id post_id author created_at
6b9b9319 77fcab6b 山田太郎
2021-12-
03T10:12:45.033Z
アクセスパターンのうちPrimary Keyで実現できない
ものをGSIとして指定する
21
attribute名を共通化する
 最終的には各モデルを統合して一つのテーブルを作るので、
各モデルのattribute名をできる限り共通化する。
 PK、SKは必ず共通化する。GSIを貼るattributeは出来る
だけ共通化した方がGSIの数を抑えることができる。
テーブル設計手法:複数のモデルを一つのテーブルに統合する
?
22
GSIの数だけI/Oのコストが増えるため、GSIの数は
最小限に抑える方がよい
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Partition key Sort key value1 value2 value3
aaa 1 taro yes ok
aaa 2 ichiro no ng
bbb 1 hanako yes ok
bbb 2 kano yes ng
ccc 1 yui no ng
ccc 2 kei yes ok
GSIの
Partition key
に指定
GSIの
Sort keyに
指定
GSI
 GSI(Global Secondary Index)=第二のインデックス
23
GSIの実態はPrimary keyを張り替えたテーブル
メインのテーブルに対する全ての変更をコピーする
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Partition key Sort key value1 value2 value3
aaa 1 taro yes ok
aaa 2 ichiro no ng
bbb 1 hanako yes ok
bbb 2 kano yes ng
Value1 value2
メインテーブル
のPartition key
メインテーブル
のSort key value3
taro yes aaa 1 ok
ichiro no aaa 2 ng
hanako yes bbb 1 ok
kano yes bbb 2 ng
メインのテーブル
GSIのテーブル
24
GSIの実態はPrimary keyを張り替えたテーブル
メインのテーブルに対する全ての変更をコピーする
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Partition key Sort key value1 value2 value3
aaa 1 taro yes ok
aaa 2 ichiro no ng
bbb 1 hanako no ok
bbb 2 kano yes ng
Value1 value2
メインテーブル
のPartition key
メインテーブル
のSort key value3
taro yes aaa 1 ok
ichiro no aaa 2 ng
hanako yes bbb 1 ok
kano yes bbb 2 ng
メインのテーブル
GSIのテーブル
25
GSIの実態はPrimary keyを張り替えたテーブル
メインのテーブルに対する全ての変更をコピーする
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Partition key Sort key value1 value2 value3
aaa 1 taro yes ok
aaa 2 ichiro no ng
bbb 1 hanako no ok
bbb 2 kano yes ng
Value1 value2
メインテーブル
のPartition key
メインテーブル
のSort key value3
taro yes aaa 1 ok
ichiro no aaa 2 ng
hanako no bbb 1 ok
kano yes bbb 2 ng
メインのテーブル
GSIのテーブル
writeコストは
(GSIの数+1)倍になる
26
分かりやすいようにattributeの順番を入れ替える
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Post
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key, GSI1PK GSI1SK,GSI2SK,GSI3SK GSI2PK GSI3PK
id type created_at author content updated_at title genre
77fcab6b post
2021-12-
03T10:06:57.650Z 和田哲也 この記事の目的は... NoSQLのテーブル... データベース
Comment
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
id post_id created_at author content updated_at
81fcab6a 77fcab6b
2021-12-
03T10:11:44.137Z 山田太郎 面白いですね
Like
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
id post_id created_at author
6b9b9319 77fcab6b
2021-12-
03T10:12:45.033Z 山田太郎
27
attribute名を共通化
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Post
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key, GSI1PK GSI1SK,GSI2SK,GSI3SK GSI2PK GSI3PK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author content updated_at title genre
77fcab6b post
2021-12-
03T10:06:57.650Z 和田哲也 この記事の目的は... NoSQLのテーブル... データベース
Comment
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author content updated_at
81fcab6a 77fcab6b
2021-12-
03T10:11:44.137Z 山田太郎 面白いですね
Like
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author
6b9b9319 77fcab6b
2021-12-
03T10:12:45.033Z 山田太郎
28
各アクセスパターンにおいて他のモデルのデータが
混ざらないことを確認する
 混ざる可能性がある場合は次のどちらかの対応をする
1. attribute名を別のものにする
2. データにモデルを表す接頭辞をつける
 基本的に1の対応をした方が実装コストが少ない。1の対応
をすることでGSIが増えてしまう場合、2の対応をした方が
良いことがある。
テーブル設計手法:複数のモデルを一つのテーブルに統合する
29
各アクセスパターンにおいて他のモデルのデータが
混ざらないことを確認する
今回の場合、
a. 指定されたidの投稿に紐づくコメントを古い順で全て
取得する
b. 指定されたidの投稿に紐づくいいねを全て取得する
c. 指定されたユーザーが作成した投稿一覧を新しい順で
取得する
の結果が混ざってしまう。
a. , b. は1の対応をするとGSIが増えてしまうため、2の対応を
します。c. は1の対応をする。
テーブル設計手法:複数のモデルを一つのテーブルに統合する
30
各アクセスパターンにおいて他のモデルのデータが
混ざらないように修正:修正前
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Post
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key, GSI1PK GSI1SK,GSI2SK,GSI3SK GSI2PK GSI3PK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author content updated_at title genre
77fcab6b post
2021-12-
03T10:06:57.650Z 和田哲也 この記事の目的は... NoSQLのテーブル... データベース
Comment
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author content updated_at
81fcab6a 77fcab6b
2021-12-
03T10:11:44.137Z 山田太郎 面白いですね
Like
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author
6b9b9319 77fcab6b
2021-12-
03T10:12:45.033Z 山田太郎
31
各アクセスパターンにおいて他のモデルのデータが
混ざらないように修正:修正後
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Post
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key, GSI1PK GSI1SK,GSI2SK,GSI3SK GSI2PK GSI3PK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK author content updated_at title genre
77fcab6b post
2021-12-
03T10:06:57.650Z 和田哲也 この記事の目的は... NoSQLのテーブル... データベース
Comment
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK content updated_at comment_author
81fcab6a
Comment_77fcab
6b
2021-12-
03T10:11:44.137Z 面白いですね 山田太郎
Like
Primary Key Attributes Attributes Attributes Attributes Attributes Attributes
Partition Key Sort Key,GSI1PK GSI1SK
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK like_author
6b9b9319 Like_77fcab6b
2021-12-
03T10:12:45.033Z 山田太郎
32
テーブルを統合
テーブル設計手法:複数のモデルを一つのテーブルに統合する
Primary Key
Attributes
Partition Key Sort Key
PK SK_GSI1PK GSI1SK_GSI2SK_GSI3SK GSI2PK GSI3PK
77fcab6b post 2021-12-03T10:06:57.650Z 和田哲也
データベー
ス
content updated_at title
この記事の目的
は...
NoSQLの
テーブ
ル...
81fcab6a
Comment_77fc
ab6b 2021-12-03T10:11:44.137Z
content updated_at
comment
_author
面白いですね 山田太郎
6b9b9319 Like_77fcab6b 2021-12-03T10:12:45.033Z
like_author
山田太郎
33
DynamoDB Localで試す
 ローカルなのでどれだけ触っても無料
 ブラウザからも操作可能
発展編
34
 DynamoDBテーブル設計方法を解説しているAWSの
ドキュメント
 https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/develop
erguide/bp-modeling-nosql-B.html
 スライドと同じ内容の記事(より詳しく書いてます)
 https://www.ais.cmc.osaka-
u.ac.jp/technology/nosql%e3%81%ae%e3%83%86%e3%83%bc%e3%
83%96%e3%83%ab%e8%a8%ad%e8%a8%88%e6%89%8b%e6%b3%9
5/
参考

DynamoDBのテーブル設計手法.pptx

Editor's Notes

  • #2 <録画開始する>