DVA-C02 Question 93
Single answerYou are developing an application that uses Amazon DynamoDB to store customer data. The table's primary key consists of a 'CustomerID' partition key and a 'OrderDate' sort key. You need to retrieve all orders placed by a specific customer within the past 30 days. Which operation is the most efficient for this use case?
- A
Use the Query operation with a key condition expression to filter by 'CustomerID' and 'OrderDate'.
- B
Use the Scan operation with a filter expression to filter by 'CustomerID' and 'OrderDate'.
- C
Use the Scan operation without any filters and manually process the results in your application code.
- D
Use the Query operation without any key condition expression and apply filters in the application layer.
Show answer and explanation
Correct answer: A
Explanation
The Query operation is designed to retrieve items based on their primary key values, making it the most efficient method for retrieving all orders for a specific 'CustomerID'. The use of a key condition expression ensures that only relevant items are returned, avoiding the overhead of scanning the entire table as required by the Scan operation.
- A. Correct.
Correct: The Query operation is more efficient because it retrieves items directly based on the primary key values. Using a key condition expression allows you to filter results by 'CustomerID' and 'OrderDate' efficiently.
- B. Incorrect.
Incorrect: The Scan operation examines every item in the table, which is inefficient compared to a Query for this use case. Even with a filter expression, the entire table must still be scanned.
- C. Incorrect.
Incorrect: This is the least efficient approach as it requires scanning the entire table and manually filtering results in your application, which increases latency and resource usage.
- D. Incorrect.
Incorrect: A Query operation without a key condition expression is invalid in this scenario because a Query requires at least the partition key ('CustomerID') to be specified. Additionally, applying filters in the application layer is inefficient.