A developer is designing a document management system where metadata is stored in an Amazon DynamoDB table with DocumentId as the partition key. The application must support two new requirements:
1. Retrieve all documents associated with a specific Department (e.g., 'HR') that were uploaded after a certain timestamp.
2. Retrieve a list of all documents that are flagged as containing malware (the IsMalicious attribute is set to true), which applies to less than 0.1% of all stored documents.
To optimize queries and minimize Read Capacity Units (RCUs) consumption, which two actions should the developer take? (Select TWO.)
- Create a Global Secondary Index (GSI) with Department as the partition key and UploadTimestamp as the sort key, and perform Query operations on this GSI.Answer
- Create a sparse Global Secondary Index (GSI) with IsMalicious as the partition key, and perform Query operations on this GSI to retrieve the flagged documents.Answer
- CUse a Scan operation on the base table with a FilterExpression containing Department and UploadTimestamp to retrieve the department documents.
- DPerform a Scan operation on the base table with a FilterExpression checking if IsMalicious is equal to true to retrieve the flagged documents.
- EEmbed a long-lived IAM Access Key and Secret Access Key directly in the AWS SDK client configuration within the application code to speed up client initialization.
Answer
Create a Global Secondary Index (GSI) with Department as the partition key and UploadTimestamp as the sort key, and create a sparse GSI with IsMalicious as the partition key, querying both indexes rather than scanning the base table.
The correct approach involves optimizing the two read access patterns using DynamoDB Query operations instead of Scan operations. For the first access pattern, creating a Global Secondary Index (GSI) with Department as the partition key and UploadTimestamp as the sort key allows the application to directly target the required items. For the second access pattern, a sparse GSI with IsMalicious as the partition key only indexes items that have this attribute set, which represents less than 0.1% of the database. Querying this sparse index is highly cost-effective and performs exceptionally fast because DynamoDB does not have to scan the non-matching items.
Step-by-Step Solution
Key Concept
Using Query operations on GSIs and sparse GSIs to optimize data retrieval and avoid Scan operations in DynamoDB.