Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is building a smart home application that records temperature readings from IoT sensors. The data is stored in an Amazon DynamoDB table where the partition key is `SensorID` and the sort key is `Timestamp`. The developer needs to retrieve all readings for a specific `SensorID` where the recorded temperature is greater than 2525. Which approach is the most efficient and cost-effective way to retrieve this data?

  1. Perform a Query operation specifying the SensorID in the KeyConditionExpression, and use a FilterExpression to return only the items where the temperature is greater than 25.Answer
  2. B
    Perform a Scan operation with a FilterExpression to check both the SensorID and whether the temperature is greater than 25.
  3. C
    Perform a Scan operation on the table and initialize the DynamoDB client in the application code using hardcoded AWS credentials to perform the temperature filtering.
  4. D
    Perform a Scan operation on the table, and resolve any resulting ProvisionedThroughputExceededException errors by increasing the provisioned Read Capacity Units (RCUs) of the table.

Answer

Perform a Query operation specifying the SensorID in the KeyConditionExpression, and use a FilterExpression to return only the items where the temperature is greater than 25.
The Query operation is the most efficient and cost-effective method to retrieve items that share a common partition key. By specifying the partition key (SensorID) in the KeyConditionExpression, DynamoDB directly accesses the partition containing the target items. The FilterExpression is then applied to the non-key temperature attribute to filter the results before they are returned to the application, minimizing payload size.

Step-by-Step Solution

1
Identify the key schema of the DynamoDB table.
The partition key is SensorID and the sort key is Timestamp.
Knowing the primary keys allows us to target queries to specific partition keys rather than scanning the table.
2
Select the correct operation for retrieving items under a specific partition key.
Use the Query operation rather than the Scan operation.
Query searches only the items matching the partition key, consuming far fewer Read Capacity Units (RCUs) than Scan.
3
Determine how to apply the condition on the non-key temperature attribute.
Apply a FilterExpression for the temperature attribute.
Since temperature is not part of the primary key, it cannot be included in the KeyConditionExpression, but must be filtered using a FilterExpression.

Key Concept

Using the Query operation with a KeyConditionExpression for the partition key and a FilterExpression for non-key attributes is the most efficient retrieval method in DynamoDB.
Rate this question