A developer is building a warehouse inventory management application that uses an Amazon DynamoDB table. The table's partition key is and the sort key is . The developer needs to retrieve all items belonging to a specific warehouse where the attribute is less than . The table contains millions of items, but each individual warehouse has at most a few thousand items. Which approach should the developer use to retrieve the required items with the lowest latency and the most efficient Read Capacity Unit () consumption?
- Perform a `Query` operation specifying the in the `KeyConditionExpression` and filtering the results using a `FilterExpression` for .Answer
- BPerform a `Scan` operation on the table using a `FilterExpression` to match the and .
- CPerform a `Scan` operation to retrieve all items in the table, and then filter out the items with a stock count greater than or equal to in the application memory.
- DInitialize the DynamoDB client with hardcoded IAM credentials in the application code, and perform a parallel `Scan` to quickly retrieve the data.
Answer
Perform a Query operation specifying the WarehouseID in the KeyConditionExpression and filtering the results using a FilterExpression for StockCount.
The correct option is to perform a Query operation specifying the partition key () in the `KeyConditionExpression` and using a `FilterExpression` for the non-key attribute (). In DynamoDB, Query operations are optimized to search within a single partition key, avoiding full table scans and reducing both latency and Read Capacity Unit (RCU) consumption. The FilterExpression acts on the retrieved partition items before they are returned to the application, ensuring that only relevant items are sent over the network.
Step-by-Step Solution
Key Concept
Choosing Query over Scan for partition key queries and filtering results using FilterExpressions to optimize performance and cost.