DVA-C02 Question 96
Single answerYou are developing a serverless application using AWS DynamoDB for a product catalog. The table has a primary key composed of a Partition Key ('Category') and a Sort Key ('ProductID'). You need to retrieve all items within a specific 'Category' and sort them by 'ProductID', while ensuring optimal query performance. Which operation should you use to achieve this?
- A
Query operation with a Partition Key filter
- B
Scan operation with a FilterExpression for 'Category'
- C
Query operation using the Partition Key and the Sort Key
- D
Scan operation to retrieve and sort all items in the table
Show answer and explanation
Correct answer: C
Explanation
The Query operation is designed to efficiently retrieve items from a DynamoDB table when you know the Partition Key and optionally use the Sort Key to refine your results. In this scenario, specifying both the Partition Key ('Category') and Sort Key ('ProductID') ensures that DynamoDB fetches and sorts only the relevant items, optimizing performance and cost. A Scan operation, on the other hand, is less efficient as it reads every item in the table, which is unnecessary for this use case.
- A. Incorrect.
Incorrect: While the Query operation is appropriate, filtering by Partition Key alone is insufficient to sort by 'ProductID'. The Sort Key should also be considered in the operation.
- B. Incorrect.
Incorrect: Scan operations are not efficient for this use case, as they read the entire table and apply filters afterward, which increases latency and costs.
- C. Correct.
Correct: Using a Query operation with both the Partition Key ('Category') and Sort Key ('ProductID') ensures that only the relevant items are retrieved and sorted efficiently.
- D. Incorrect.
Incorrect: A Scan operation reads the entire table, which is unnecessary and inefficient for retrieving a subset of data based on a specific Partition Key and Sort Key.