Question

Difficulty: EasyData Store Operations with Amazon DynamoDB

A developer is building a blogging application where posts are stored in an Amazon DynamoDB table. The table's partition key is AuthorId and the sort key is PostId. The application needs to retrieve all posts written by a specific author that were published within the last 30 days. This operation must minimize latency and read capacity unit (RCU) consumption. Which combination of actions should the developer take to meet these requirements? (Select TWO.)

  1. Create a Global Secondary Index (GSI) with AuthorId as the partition key and PublishDate as the sort key.Answer
  2. Perform a Query operation on the Global Secondary Index (GSI) using a KeyConditionExpression to specify the AuthorId and PublishDate range.Answer
  3. C
    Perform a Scan operation on the base table using a FilterExpression to filter by AuthorId and PublishDate.
  4. D
    Perform a Query operation on the base table using a FilterExpression for both AuthorId and PublishDate.
  5. E
    Initialize the Amazon DynamoDB client by hardcoding the AWS access keys in the application source code.

Answer

To optimize the retrieval, the developer should create a Global Secondary Index (GSI) with AuthorId as the partition key and PublishDate as the sort key, and then perform a Query operation on that GSI using a KeyConditionExpression for both fields.
To optimize read capacity unit (RCU) consumption and reduce latency, the query should target only the relevant data. Creating a Global Secondary Index (GSI) with AuthorId as the partition key and PublishDate as the sort key enables querying by date. Querying the GSI using a KeyConditionExpression for both AuthorId and PublishDate ensures that DynamoDB reads only the items that match the criteria.

Step-by-Step Solution

1
Analyze the table key schema and query requirements.
The base table uses AuthorId as the partition key and PostId as the sort key, but the query needs to filter by AuthorId and PublishDate.
Since PublishDate is not part of the primary key, querying the base table directly by date would require filtering all items for a partition, which is inefficient.
2
Design a Global Secondary Index (GSI) to support the query.
Create a GSI with AuthorId as the partition key and PublishDate as the sort key.
This configuration allows the application to perform key-based lookups on the date range for specific authors.
3
Use the Query API with KeyConditionExpression.
Query the GSI, supplying the AuthorId and the 30-day range for PublishDate in the KeyConditionExpression.
Using KeyConditionExpression on the GSI ensures that DynamoDB only reads matching items, keeping RCU usage low.

Key Concept

Using a Global Secondary Index (GSI) with a KeyConditionExpression to optimize queries on non-primary key attributes and minimize RCU consumption.
Rate this question