An online education company uses an Amazon DynamoDB table named `CourseProgress` to track student progress. The table uses `StudentID` as the partition key and `CourseID` as the sort key. Over time, students accumulate thousands of records, but only a small fraction of these courses are fully completed. A dashboard frequently retrieves only the completed courses for a specific student. As the volume of in-progress course records grows, the dashboard queries become slower and consume a significant number of Read Capacity Units (RCUs). Which database design pattern or operation should a developer implement to retrieve the completed courses in the most cost-effective and performant manner?
- Create a Global Secondary Index (GSI) with `StudentID` as the partition key and a new attribute `CompletedDate` as the sort key. Only populate the `CompletedDate` attribute in the base table when a course is completed, and query the GSI to retrieve the records.Cevap
- BPerform a Scan operation on the base table with a FilterExpression that filters the results where the partition key matches the target student and the progress status is 'Completed'.
- CQuery the base table using the `StudentID` partition key with a FilterExpression for completed courses, and increase the table's provisioned Read Capacity Units (RCUs) to prevent ProvisionedThroughputExceededException errors during peak times.
- DImplement parallel Scan operations using the AWS SDK, initializing the client with hardcoded IAM credentials that have read-only access to speed up the data retrieval process.
Cevap
Create a Global Secondary Index (GSI) with StudentID as the partition key and a new attribute CompletedDate as the sort key. Only populate the CompletedDate attribute in the base table when a course is completed, and query the GSI to retrieve the records.
Creating a Global Secondary Index (GSI) with the student identifier as the partition key and a completion date as the sort key, and only populating this completion date when the course is finished, creates a sparse index. In Amazon DynamoDB, items that do not contain the GSI's sort key attribute are not indexed. As a result, the GSI only contains records for completed courses, allowing the application to perform highly efficient Queries against the GSI that consume Read Capacity Units (RCUs) only for the completed courses, rather than reading and filtering all in-progress records.
Adım Adım Çözüm
Anahtar Kavram
DynamoDB Sparse Indexes and Query Optimization