DVA-C02 · Question #377
A developer is creating a web application for a school that stores data in Amazon DynamoDB. The ExamScores table has the following attributes: student_id, subject_name, and top_score. Each item in the
The correct answer is C. Create a global secondary index (GSI) with subject_name as the partition key and top_score as. A Global Secondary Index (GSI) with subject_name as the partition key and top_score as the sort key enables efficient queries that retrieve items for a given subject sorted by score descending, yielding the top scorer.
Question
A developer is creating a web application for a school that stores data in Amazon DynamoDB. The ExamScores table has the following attributes: student_id, subject_name, and top_score. Each item in the ExamScores table is identified with student_id as the partition key and subject_name as the sort key. The web application needs to display the student _id for the top scores for each school subject. The developer needs to increase the speed of the queries to retrieve the student_id for the top scorer for each school subject. Which solution will meet these requirements?
Options
- ACreate a local secondary index (LSI) with subject_name as the partition key and top_score as the
- BCreate a local secondary index (LSI) with top_score as the partition key and student_id as the
- CCreate a global secondary index (GSI) with subject_name as the partition key and top_score as
- DCreate a global secondary index (GSI) with subject_name as the partition key and student_id as
How the community answered
(31 responses)- A16% (5)
- B3% (1)
- C74% (23)
- D6% (2)
Why each option
A Global Secondary Index (GSI) with subject_name as the partition key and top_score as the sort key enables efficient queries that retrieve items for a given subject sorted by score descending, yielding the top scorer.
An LSI cannot change the partition key away from student_id; it must share the base table partition key, so a query on subject_name as the partition key is not possible with an LSI.
An LSI with top_score as the partition key is invalid because an LSI must use the same partition key as the base table (student_id), and using top_score as a partition key distributes data poorly.
With subject_name as the GSI partition key, all exam scores for a given subject are grouped together in the index. Using top_score as the sort key means DynamoDB stores them in score order, allowing the application to query the GSI for a subject and retrieve items sorted by top_score efficiently. The student_id attribute is projected into the index and returned in the query results.
A GSI with subject_name as partition key and student_id as sort key groups items by subject but sorts by student_id, not score, making it impossible to efficiently identify the top scorer without reading and sorting all items in application code.
Concept tested: DynamoDB GSI design for top-N queries by sort key
Source: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
Community Discussion
No community discussion yet for this question.