A developer is building a supply chain shipment tracking application that stores transit logs in an Amazon DynamoDB table. Each item contains metadata about a shipment, including ShipmentID (partition key), TransitTime (sort key), and the current WarehouseID where the shipment is located. The application needs to retrieve all transit logs for a specific shipment that occurred within the last 48 hours. The table contains over 10 million items. Which of the following approaches is the most performant and cost-effective method to retrieve the required transit logs?
- Perform a Query operation on the table using a KeyConditionExpression for the ShipmentID and a range comparison on the TransitTime attribute.Answer
- BPerform a Scan operation on the table and use a FilterExpression on ShipmentID and TransitTime to filter the results before returning them to the application.
- CPerform a Scan operation, and resolve any resulting ProvisionedThroughputExceededException by increasing the table's provisioned Read Capacity Units (RCUs).
- DPerform a Scan operation, but initialize the AWS SDK client with hardcoded AWS access keys directly in the initialization code to bypass IAM role evaluation overhead.
Answer
Perform a Query operation on the table using a KeyConditionExpression for the ShipmentID and a range comparison on the TransitTime attribute.
Performing a Query operation with a KeyConditionExpression is the most performant and cost-effective approach. DynamoDB directly targets the partition associated with the partition key (ShipmentID) and uses the sort key (TransitTime) to narrow down the results, consuming RCUs only for the items read rather than the entire table.
Step-by-Step Solution
Key Concept
Selecting Query over Scan for efficient data retrieval in DynamoDB using the primary key structure.