A logistics company uses an Amazon DynamoDB table to store tracking updates for shipments. The table uses ShipmentID as the partition key and Timestamp as the sort key. The application needs to retrieve all tracking updates for a specific ShipmentID that occurred within a given 48-hour window. The table contains millions of items representing shipments from the last year. How should the developer implement this retrieval to optimize read throughput and minimize latency?
- Perform a Query operation specifying the ShipmentID in the KeyConditionExpression and a range condition on the Timestamp sort key.Answer
- BPerform a Scan operation on the table and apply a FilterExpression on ShipmentID and Timestamp to filter out the undesired shipments and dates.
- CPerform a parallel Scan operation using segments to divide the table, and filter the returned items using the application code.
- DInitialize the DynamoDB client by embedding an IAM User's access key and secret key directly in the client configuration, and execute a Scan operation.
Answer
Perform a Query operation specifying the ShipmentID in the KeyConditionExpression and a range condition on the Timestamp sort key.
Performing a Query operation specifying the ShipmentID in the KeyConditionExpression and a range condition on the Timestamp sort key is the most efficient method. A Query directly locates the items using the partition key index and filters them based on the sort key range, consuming Read Capacity Units (RCUs) only for the returned items.
Step-by-Step Solution
Key Concept
Query vs Scan operations and key design in DynamoDB
Estimated Time:1m 30s