

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# ステップ 6: データクエリを作成する
<a name="step6-hierarchical-model"></a>

アクセスパターンを定義してデータモデルを設計したら、DynamoDB データベースの階層データをクエリできます。コストを抑えてパフォーマンスを確保するためのベストプラクティスとして、次の例では `Scan` がないクエリオペレーションのみを使用しています。
+ **コンポーネントの祖先を検索します。**

  CM8 コンポーネントの祖先 (親、祖父母、曽祖父母など) を見つけるには、`ComponentId = "CM8"` を使用してベーステーブルをクエリします。クエリは次のレコードを返します。

  結果データのサイズを小さくするには、プロジェクション式を使用して `Path` 属性だけを返すことができます。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)
+ **コンポーネントの直接の子を検索します。**

  CM2 コンポーネントのすべての直接の子コンポーネント、または 1 レベルの下流コンポーネントを取得するには、`ParentId = "CM2"` を使用して GSI1 にクエリを実行します。クエリは次のレコードを返します。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)
+ **最上位コンポーネントを使用して、すべての下流の子コンポーネントを検索します。**

  最上位コンポーネント (CM1) のすべての子コンポーネント、または下流コンポーネントを取得するには、`GraphId = "CM1#1"` と `begins_with("Path", "CM1|")` を使用して GSI2 にクエリを実行し、`ComponentId` でプロジェクション式を使用します。すると、そのツリーに関連するすべてのコンポーネントが返されます。

  この例では、CM1 を最上位コンポーネントとするツリーが 1 つありますが、実際には、同じテーブルに数百万もの最上位コンポーネントがある可能性があります。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)
+ **中間レベルのコンポーネントを使用して、下流のすべての子コンポーネントを検索します。**

  コンポーネント (CM2) のすべての子または下流コンポーネントを再帰的に取得するには、2 つの方法があります。レベルごとに再帰的にクエリを実行するか、GSI2 インデックスにクエリを実行することができます。
  + 子コンポーネントの最後のレベルに達するまで、GSI1 をレベルごとに再帰的にクエリします。

    1. `ParentId = "CM2"` を使用して GSI1 にクエリを実行すると、次のようなレコードが返ってきます。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. 再度、`ParentId = "CM4"` を使用して GSI1 にクエリを実行します。次のようなレコードが返ってきます。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. 再度、`ParentId = "CM5"` を使用して GSI1 にクエリを実行します。次のようなレコードが返ってきます。

       これを繰り返します。最後のレベルに到達するまで、それぞれの `ComponentId` に対してクエリを実行します。`ParentId = "<ComponentId>"` を使用したクエリに結果が返されなくなったら、前の結果がツリーの最後のレベルからの結果となります。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. すべての結果をマージします。

        

       結果=[CM4, CM5] \$1 [CM8, CM9] \$1 [CM10]

                =[CM4, CM5, CM8, CM9, CM10]
  + 最上位コンポーネント (自動車、または CM1) の階層ツリーを格納する GSI2 をクエリします。

    1. まず、最上位のコンポーネントまたは上位の祖先、および CM2 の `Path` を検索します。検索方法は、`ComponentId = "CM2"` を使用してベーステーブルにクエリを実行し、階層ツリー内のそのコンポーネントのパスを検索します。`GraphId` 属性と `Pat`h 属性を選択します。クエリは次のレコードを返します。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. `GraphId = "CM1#1" AND BEGINS_WITH("Path", "CM1|CM2|")` を使用して GSI2 にクエリを実行します。クエリは次の結果を返します。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. CM2 のすべての子コンポーネントを返すには、`ComponentId` 属性を選択します。