Share
Embed
Transcript
82 slides
-
Copyright © RevComm Inc. • 大谷 紗良(Sara Ohtani) • Software
Engineer, Backend ◦ • RevComm Inc. • @sara_ohtani_mt2 • #ya8 #ya8B 自己紹介 2 -
Copyright © RevComm Inc. awsのNoSQL DB、DynamoDBはスピードやコストの点で期待が高い でもなかなか利用シーンが限られているようで・・・ でもでもよく調べると色々できそうで・・・ でもでもでも試したらやはりちょっとできなかった、という話 今日の話
この資料は slide share公開済みです https://tech.revcomm.co.jp/partial-match-search-with-dynamodb 4 -
Copyright © RevComm Inc. データ量の多さとそれを起因とする遅さ • 1テナントにつき最大50万件の連絡先 ※現在最大75万件 • できれば1秒以下で取得したい
📞 🐢... 📇 リニューアルするにあたって意識している課題 16 -
Copyright © RevComm Inc. DynamoDBのデータ { "id": { "N": “1”
}, "name": { "S": "John" }, "type": { "S": "dog" } } Partition Key Sort Key (※option) その他 Attributes id name 1 John type dog item 21 -
Copyright © RevComm Inc. Scan検索でなくQuery検索を使う • Query検索は全体のデータ量にあまり影響を受けず速い • しかしQuery検索は柔軟な検索ができない •
余談 ◦ Query検索で一度に取得できるデータは1MBまで ◦ 一度で取得できなかったときには LastEvaluatedKeyに値が入ってくる 速さを活かすためには 22 -
Copyright © RevComm Inc. Scan検索でなくQuery検索を使う • Query検索は全体のデータ量にあまり影響を受けず速い • しかしQuery検索は柔軟な検索ができない •
余談 ◦ Query検索で一度に取得できるデータは1MBまで ◦ 一度で取得できなかったときには LastEvaluatedKeyに値が入ってくる 速さを活かすためには 23 -
Copyright © RevComm Inc. Keyで絞り込んで該当itemが取得できる DynamoDBのQuery検索とは Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ item 24 -
Copyright © RevComm Inc. Keyで絞り込んで該当itemが取得できる DynamoDBのQuery検索 Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ item Query検索の場合、絞り込み条件として Partition Keyは必ず指定しなければならない 完全一致検索のみ 25 -
Copyright © RevComm Inc. Keyで絞り込んで該当itemが取得できる DynamoDBのQuery検索 Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ idわからないけどnameが Johnのデータが欲しいな 26 -
Copyright © RevComm Inc. Keyで絞り込んで該当itemが取得できる DynamoDBのQuery検索 Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ idわからないけどnameが Johnのデータが欲しいな 27 -
Copyright © RevComm Inc. Keyで絞り込んで該当itemが取得できる DynamoDBのQuery検索 Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ item Sort KeyはPKを指定した上であれば 「EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN」で絞り込める (Sort Keyの指定はオプション) 28 -
Copyright © RevComm Inc. Keyで絞り込んで該当itemが取得できる DynamoDBのQuery検索 Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ item KeyとKey以外はQuery検索で扱う上で 全く別物といっていい 29 -
Copyright © RevComm Inc. 1item内にフラットに情報を並べるのではなく、 情報を縦に持つ GSIを活用する前提の設計の考え方 PK, GSI-SK Sort
Key その他Attributes ID DataType 1 Name DataValue John ・・・ ・・・ ・・・ ・・・ ・・・ 1 Type dog ・・・ ・・・ GSI-PK Primary Table 32 -
Copyright © RevComm Inc. 1item内で横に情報を持つイメージ DynamoDBのQuery検索とは Partition Key Sort Key
その他Attributes id name 1 John type dog ・・・ ・・・ ・・・ ・・・ ・・・ 2 Paul cat ・・・ ・・・ item 33 -
Copyright © RevComm Inc. 1item内にフラットに情報を並べるのではなく、 情報を縦に持つ GSIを活用する前提の設計の考え方 PK, GSI-SK Sort
Key その他Attributes ID DataType 1 Name DataValue John ・・・ ・・・ ・・・ ・・・ ・・・ 1 Type dog ・・・ ・・・ GSI-PK Primary Table 34 -
Copyright © RevComm Inc. 1item内にフラットに情報を並べるのではなく、 情報を縦に持つ 使うとどうなる? GSI-PK GSI-SK Projected
Attributes DataValue ID John 1 DataType Name ・・・ ・・・ ・・・ ・・・ ・・・ dog 1 Type ・・・ ・・・ (PK) dog 3 Type GSI Table 35 -
Copyright © RevComm Inc. Query検索の考え方は同じ 使うとどうなる? GSI-PK GSI-SK Projected Attributes
DataValue ID John 1 DataType Name ・・・ ・・・ ・・・ ・・・ ・・・ dog 1 Type ・・・ ・・・ (PK) dog 3 Type GSI Table idわからないけどnameが Johnのデータが欲しいな ⭕ 36 -
Copyright © RevComm Inc. Query検索の考え方は同じ 使うとどうなる? GSI-PK GSI-SK Projected Attributes
DataValue ID John 1 DataType Name ・・・ ・・・ ・・・ ・・・ ・・・ dog 1 Type ・・・ ・・・ (PK) dog 3 Type GSI Table dogのデータ・・・⭕ 37 -
Copyright © RevComm Inc. それはそう でも部分一致検索したい 完全一致か 前方一致じゃ だめですよね? 😉
PO: それはちょっと・・・ 😉 41 -
Copyright © RevComm Inc. 1. データ抽出条件が完全一致、あるいは前方一致で良い 2. データ件数が多く、今後もさらに増加が予想される 3. アクセスパターンや要件がはっきりしている
4. データ検索する際の絞り込み条件となる項目が多くない 5. 基本的にテーブルをjoinする必要がない 6. 並び順は問わない DynamoDBがマッチすると思われるユースケース 48 -
Copyright © RevComm Inc. 1. 業務分析とデータのモデリング 2. アクセスパターン設計 3. テーブルとインデックス設計
4. クエリ条件設計 DynamoDBのテーブル設計の流れ 53 -
Copyright © RevComm Inc. 1処理ずつのアクセスだけでなく、 バッチ処理でそれぞれのアクセスが パーティションに集中してもホットになる ホットパーティションとは ID DataType
1 Name DataValue John 1 Type dog 2 Paul cat batchWriter AWS Lambda Amazon DynamoDB 61 -
Copyright © RevComm Inc. テーブル・インデックス設計 DataType: なんのデータ#どの連絡先と紐づいてるか SearchType: 検索の種類#テナントid SearchValue:
"なんのデータ"の値 CreatedAt(ソートしたい項目): id取得のための検索itemと基本情報itemにだけセットすればいい 66 -
Copyright © RevComm Inc. このクエリ条件をもとに実際に動かしてテストしていく クエリ条件設計 機能 Entity UseCase Lookup
parameters order by Table/Index Key Filter 一覧表示 contacts, contact_sample getContactsByTenantId, getContactSampleByContactId 1. {tenant_id}, 2. {contact_id} created_at (DESC) 1. GSI 2. Primary Table 1. SearchType = “FreeWord#Tenant_{acco unt_tenant_id}” 2. ID = :contact_id and DataType = “Tenant_{account_tenant _id}#Contacts” - 一覧表示 (検索) contacts, contact_sample getContactsByTenantId, getContactSampleByContactId 1. {tenant_id}, 2. {contact_id} created_at (DESC) 1. GSI 2. Primary Table 1. SearchType = “FreeWord#Tenant_{acco unt_tenant_id}” and SearchValue BEGINS_WITH :tenant_id”#”:param 2. ID = :contact_id and DataType BEGINS_WITH “Contacts#Tenant_{acco unt_tenant_id}#Contact_” - 個別表示 contacts, contact_sample ・ ・ ・ 電話発信 contacts 商談後情報入力 contact_sample ・ ・ ・ 68 -
Copyright © RevComm Inc. このクエリ条件をもとに実際に動かしてテストしていく クエリ条件設計 機能 Entity UseCase Lookup
parameters order by Table/Index Key Filter 一覧表示 contacts, contact_sample getContactsByTenantId, getContactSampleByContactId 1. {tenant_id}, 2. {contact_id} created_at (DESC) 1. GSI 2. Primary Table 1. SearchType = “FreeWord#Tenant_{acco unt_tenant_id}” 2. ID = :contact_id and DataType = “Tenant_{account_tenant _id}#Contacts” - 一覧表示 (検索) contacts, contact_sample getContactsByTenantId, getContactSampleByContactId 1. {tenant_id}, 2. {contact_id} created_at (DESC) 1. GSI 2. Primary Table 1. SearchType = “FreeWord#Tenant_{acco unt_tenant_id}” and SearchValue BEGINS_WITH :tenant_id”#”:param 2. ID = :contact_id and DataType BEGINS_WITH “Contacts#Tenant_{acco unt_tenant_id}#Contact_” - 個別表示 contacts, contact_sample ・ ・ ・ 電話発信 contacts 商談後情報入力 contact_sample ・ ・ ・ テーブル設計とクエリ条件設計を往復・・・ 69 -
Copyright © RevComm Inc. 一覧表示(検索)のサンプルコードはこちら 主な流れ 1. GSI経由で検索用データから条件に一致する contact_id一覧を取得する 2.
取得したcontact_id一覧から基本データを取得する • 部分一致検索の書き方を検索していると今は非推奨の古い書き方例がかなりよく出てくるので注意 • SDKのresourceは古いため非推奨となっており、代わりにclientを使うことが推奨されている データ取得イメージ 70 -
Copyright © RevComm Inc. 1. データ抽出条件が完全一致、あるいは前方一致で良い 2. データ件数が多く、今後もさらに増加が予想される 3. アクセスパターンや要件がはっきりしている
4. データ検索する際の絞り込み条件となる項目が多くない 5. 基本的にテーブルをjoinする必要がない 6. 並び順は問わない あらためてDynamoDBがマッチすると思われるユースケース 72 -
Copyright © RevComm Inc. SQLでいうところのorder byがない Sort Key順以外の並び順にしたいときは 一度全検索結果のidと並び順条件の情報を取得した上で アプリ側でソートすることになる
そうすると本当は1MBまでで取れるデータだけでいいところが 全結果を取得しないといけなくなる 並び順はいっそ選べないと思っていたほうが良さそう 6. 並び順は問わない 78 -
Copyright © RevComm Inc. • 特に参考にした記事 ◦ https://speakerdeck.com/_kensh/dynamodb-design-practice ◦ https://speakerdeck.com/handslabinc/dynamodbdemojian-suo-sitai
• DynamoDBのテーブル設計における多対多の考え方 ◦ https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/bp-adja cency-graphs.html ◦ https://hack-le.com/dynamodb-many-to-many/ • ホットパーティションについて ◦ https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/bp-part ition-key-uniform-load.html • クエリのパフォーマンスと継続した負荷に対してDynamoDBはどのように対応するか検証記事 ◦ https://aws.amazon.com/jp/blogs/news/part-2-scaling-dynamodb-how-partitions-ho t-keys-and-split-for-heat-impact-performance/ 参考