A developer is building a recipe sharing platform. The recipes are stored in an Amazon DynamoDB table with `RecipeID` as the partition key. Each recipe item contains attributes such as `Title`, `PrepTime`, and `Category` (e.g., 'Dessert', 'Main Course'). The application homepage needs to display all recipes in the 'Dessert' category. The developer wants to retrieve these items while minimizing the latency and the consumption of Read Capacity Units (RCUs).
Which approach should the developer take to retrieve these recipes?
- APerform a `Scan` operation on the base table with a `FilterExpression` to retrieve only the items where `Category` equals 'Dessert'.
- Create a Global Secondary Index (GSI) with `Category` as the partition key, and perform a `Query` operation on the GSI using the category value.Cevap
- CIncrease the provisioned Read Capacity Units (RCUs) of the base table to prevent throttling, and execute a `Scan` operation on the table to filter for the 'Dessert' category in the application memory.
- DInitialize the AWS SDK client inside the application using hardcoded AWS credentials to minimize IAM authorization latency, and perform a `Scan` operation on the base table.
Cevap
Create a Global Secondary Index (GSI) with `Category` as the partition key, and perform a `Query` operation on the GSI using the category value.
The correct answer is to create a Global Secondary Index (GSI) with `Category` as the partition key and query the GSI. Because the base table's partition key is `RecipeID`, querying by `Category` is not directly supported on the base table. By creating a GSI, you can perform a `Query` operation that target-retrieves only the items matching the 'Dessert' category. This consumes RCUs only for the matched items and projected attributes, offering low latency and maximum cost-efficiency.
Adım Adım Çözüm
Anahtar Kavram
Using Global Secondary Indexes (GSIs) to enable query operations on non-key attributes in Amazon DynamoDB.