Soru

Zorluk: OrtaData Store Operations with Amazon DynamoDB

A developer is building a smart home energy monitoring dashboard. The energy usage data is stored in an Amazon DynamoDB table where the partition key is SmartMeterID and the sort key is ReadingTimestamp. The developer needs to retrieve all energy consumption readings for a specific SmartMeterID over the last 7 days to display them on a user dashboard.

Which of the following approaches is the most performant and cost-effective method to retrieve these records?

  1. Execute a Query operation with a KeyConditionExpression that specifies the SmartMeterID and uses a comparison operator on the ReadingTimestamp sort key.Cevap
  2. B
    Perform a Scan operation across the entire table, applying a FilterExpression to filter by the target SmartMeterID and ReadingTimestamp range.
  3. C
    Instantiate the DynamoDB client using hardcoded IAM access keys in the code to ensure permissions are resolved quickly, then run a Scan operation with a filter.
  4. D
    Perform a Scan operation, and if a ProvisionedThroughputExceededException occurs, scale up the overall provisioned Read Capacity Units (RCUs) for the table.

Cevap

Execute a Query operation with a KeyConditionExpression that specifies the SmartMeterID and uses a comparison operator on the ReadingTimestamp sort key.
The Query operation is the most efficient and cost-effective way to retrieve items from a DynamoDB table when the partition key is known. By specifying the partition key (SmartMeterID) and using a comparison operator on the sort key (ReadingTimestamp) in the KeyConditionExpression, DynamoDB only reads the items that match the criteria, minimizing the consumed Read Capacity Units (RCUs).

Adım Adım Çözüm

1
Identify the primary key structure of the Amazon DynamoDB table.
The partition key is SmartMeterID, and the sort key is ReadingTimestamp.
Knowing the primary key structure determines which retrieval operations (GetItem, Query, or Scan) are supported and efficient.
2
Determine the data retrieval requirements.
The query needs to target a specific SmartMeterID and a specific range of ReadingTimestamp values (the last 7 days).
This requirement matches the exact structure of a Query operation, which requires an equality comparison on the partition key and optional range comparisons on the sort key.
3
Select the most efficient operation and define the parameters.
Use the Query API with a KeyConditionExpression that specifies the SmartMeterID and a comparison operator (such as BETWEEN or >) on ReadingTimestamp.
A Query operation only searches and consumes Read Capacity Units (RCUs) for the partition and matching sort key range, unlike a Scan which processes the entire table.

Anahtar Kavram

Efficient data retrieval using the DynamoDB Query API vs Scan API
Bu soruyu puanla