Question

Difficulty: MediumData Store Operations with Amazon DynamoDB

A developer is building an e-learning application. The application tracks student progress in an Amazon DynamoDB table named `CourseEnrollments`. The table uses `StudentID` as the partition key and `CourseID` as the sort key. The table contains attributes such as `CompletionPercentage` and `LastAccessedDate`. The developer needs to retrieve all progress records for a specific student where the `CompletionPercentage` is greater than 80%80\%. Which approach should the developer take to retrieve these records while minimizing the Read Capacity Units (RCUs) consumed?

  1. Perform a `Query` operation specifying the `StudentID` in the `KeyConditionExpression`, and use a `FilterExpression` to evaluate the `CompletionPercentage`.Answer
  2. B
    Perform a `Scan` operation on the table, specifying both the `StudentID` and `CompletionPercentage` in the `FilterExpression`.
  3. C
    Perform a `Scan` operation on the table, specifying a `ProjectionExpression` to limit the returned attributes, and filter by `CompletionPercentage` in the application logic.
  4. D
    Initialize the DynamoDB client with hardcoded AWS access keys, and perform a `Scan` operation to retrieve the records.

Answer

Perform a `Query` operation specifying the `StudentID` in the `KeyConditionExpression`, and use a `FilterExpression` to evaluate the `CompletionPercentage`.
The correct approach is to perform a `Query` operation specifying the partition key (`StudentID`) in the `KeyConditionExpression`, and then use a `FilterExpression` to narrow the results based on the `CompletionPercentage`. A `Query` operation only reads items that match the specified partition key, which significantly reduces the amount of data read and the number of Read Capacity Units (RCUs) consumed compared to a table scan. The `FilterExpression` is applied after the query reads the items from the partition but before returning the results to the application.

Step-by-Step Solution

1
Identify the primary key structure of the DynamoDB table.
The table has a composite primary key consisting of a partition key (`StudentID`) and a sort key (`CourseID`).
Understanding the primary key structure allows the developer to choose the most efficient data retrieval operation.
2
Compare the efficiency of `Query` and `Scan` operations for retrieving data for a specific partition key.
A `Query` operation directly targets the partition for the specified `StudentID`, whereas a `Scan` operation evaluates every item in the entire table.
Restricting the read operation to a single partition using `Query` minimizes RCU consumption.
3
Apply filtering for the non-key attribute `CompletionPercentage`.
A `FilterExpression` is applied to discard items where the completion percentage is not greater than 80%80\% after the `Query` retrieves the partition items.
Since `CompletionPercentage` is not part of the primary key, it cannot be included in the `KeyConditionExpression` and must be evaluated using a `FilterExpression`.

Key Concept

DynamoDB Query vs Scan efficiency and RCU optimization
Estimated Time:1m 30s
Rate this question