DVA-C02 Question 94
Single answerYou are developing a serverless application using DynamoDB to store customer order data. The Orders table has a partition key CustomerId and a sort key OrderDate. You need to retrieve all orders for a specific customer within the last 30 days. Which operation should you use for optimal performance?
- A
Query operation with a filter expression to specify the date range.
- B
Scan operation with a filter expression to specify the date range.
- C
Query operation specifying the partition key and a key condition expression for the date range.
- D
Scan operation specifying the partition key and a key condition expression for the date range.
Show answer and explanation
Correct answer: C
Explanation
To efficiently retrieve items from a DynamoDB table, the Query operation should be used when you know the partition key. By providing a key condition expression, you can further limit the results using the sort key. In this scenario, the Query operation with the partition key (CustomerId) and a key condition expression for the date range on the sort key (OrderDate) ensures optimal performance. Scan operations, on the other hand, are less efficient as they read the entire table and are generally not suitable for specific queries.
- A. Incorrect.
Query operation with a filter expression is less efficient because filter expressions are applied after fetching all matching items. This does not leverage the index effectively for performance.
- B. Incorrect.
Scan operation is not optimal because it reads the entire table, which is both time-consuming and computationally expensive.
- C. Correct.
Query operation specifying the partition key and a key condition expression is the correct approach, as it retrieves only the items matching the specified partition key (
CustomerId) and efficiently filters the results based on the date range using the sort key. - D. Incorrect.
Scan operation does not allow specifying the partition key and key condition expressions together. Additionally, it scans the entire table, which is inefficient for this use case.