Why Secondary Indexes Enable Flexible Queries in DynamoDB
📖 Scenario: You are managing a DynamoDB table that stores information about books in a library. Each book has a unique BookID, a Title, an Author, and a Genre. You want to be able to quickly find books by their BookID, but also want to search by Author and Genre without scanning the entire table.
🎯 Goal: Build a DynamoDB table with a primary key and add a secondary index to enable flexible queries by Author and Genre.
📋 What You'll Learn
Create a DynamoDB table named Books with BookID as the primary key.
Add a Global Secondary Index (GSI) named AuthorGenreIndex with Author as the partition key and Genre as the sort key.
Insert sample book items with exact attributes: BookID, Title, Author, and Genre.
Write a query using the GSI to find all books by a specific Author and Genre.
💡 Why This Matters
🌍 Real World
Libraries, bookstores, and content platforms often need to search items by multiple attributes quickly. Secondary indexes let them do this without scanning the entire database.
💼 Career
Understanding how to create and use secondary indexes in DynamoDB is essential for backend developers and database administrators working with NoSQL databases to build efficient, scalable applications.
Progress0 / 4 steps
1
Create the DynamoDB table with primary key
Create a DynamoDB table named Books with BookID as the primary key. Use the AWS CLI command aws dynamodb create-table with the attribute definitions and key schema exactly as shown.
DynamoDB
Hint
Use BookID as the HASH key in the key schema and define it as a string attribute.
2
Add a Global Secondary Index for Author and Genre
Modify the table creation command to add a Global Secondary Index named AuthorGenreIndex with Author as the partition key and Genre as the sort key. Include Author and Genre in the attribute definitions.
DynamoDB
Hint
Remember to add Author and Genre to the attribute definitions and define the GSI with Author as HASH and Genre as RANGE keys.
3
Insert sample book items
Insert three book items into the Books table with these exact attributes and values: 1. {"BookID": "B1", "Title": "The Great Gatsby", "Author": "F. Scott Fitzgerald", "Genre": "Fiction"} 2. {"BookID": "B2", "Title": "To Kill a Mockingbird", "Author": "Harper Lee", "Genre": "Fiction"} 3. {"BookID": "B3", "Title": "A Brief History of Time", "Author": "Stephen Hawking", "Genre": "Science"} Use the AWS CLI put-item command for each item.
DynamoDB
Hint
Use the exact attribute names and values for each book. Each put-item command inserts one book.
4
Query books by Author and Genre using the GSI
Write an AWS CLI query command to find all books by Author "Harper Lee" and Genre "Fiction" using the AuthorGenreIndex Global Secondary Index. Use the exact index name and key condition expression with placeholders.
DynamoDB
Hint
Use the query command with --index-name AuthorGenreIndex and the correct key condition expression to filter by Author and Genre.
Practice
(1/5)
1. What is the main benefit of using secondary indexes in DynamoDB?
easy
A. They allow querying data using different keys without changing the main table
B. They automatically backup your data
C. They increase the storage capacity of the table
D. They encrypt data for security
Solution
Step 1: Understand the role of secondary indexes
Secondary indexes let you query the table using keys other than the primary key.
Step 2: Compare other options
Options A, B, and C describe features unrelated to secondary indexes.
Final Answer:
They allow querying data using different keys without changing the main table -> Option A
Quick Check:
Secondary indexes = flexible queries [OK]
Hint: Secondary indexes = new query keys without table changes [OK]
Common Mistakes:
Confusing secondary indexes with backups
Thinking indexes increase storage size
Assuming indexes encrypt data
2. Which of the following is the correct syntax to create a Global Secondary Index (GSI) in a DynamoDB table definition?
Step 1: Identify correct KeyType for GSI partition key
For a GSI, the partition key uses KeyType 'HASH'. {"IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}} uses 'HASH'.
Step 2: Check Projection type
{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}} uses 'ALL', which is valid. Other options use invalid KeyTypes like 'PRIMARY' or 'SORT'.
C. An error because the query does not specify the primary key
D. All items where the Category attribute equals 'Books' using the GSI
Solution
Step 1: Understand the use of IndexName in the query
Specifying IndexName means the query uses the GSI on 'Category'.
Step 2: Analyze KeyConditionExpression
The query filters items where 'Category' equals 'Books' using the GSI.
Final Answer:
All items where the Category attribute equals 'Books' using the GSI -> Option D
Quick Check:
Query with GSI filters by Category = 'Books' [OK]
Hint: Query with IndexName uses that index's keys [OK]
Common Mistakes:
Assuming query filters primary key without IndexName
Expecting error without primary key in main table
Thinking query returns all items
4. You tried to query a DynamoDB table using a secondary index but got an error: ValidationException: Query condition missed key schema element. What is the most likely cause?
medium
A. You forgot to create the secondary index before querying
B. You used the primary key instead of the secondary index name
C. You did not specify the partition key attribute of the secondary index in the query
D. You used a Scan operation instead of Query
Solution
Step 1: Understand the error message
The error means the query is missing a required key attribute from the index's key schema.
Step 2: Identify the missing key
When querying a secondary index, you must specify its partition key attribute in the KeyConditionExpression.
Final Answer:
You did not specify the partition key attribute of the secondary index in the query -> Option C
Quick Check:
Missing index partition key causes ValidationException [OK]
Hint: Always include index partition key in query condition [OK]
Common Mistakes:
Using primary key attributes instead of index keys
Querying before index creation
Confusing Scan with Query operation
5. You have a DynamoDB table with a primary key on UserId and a Global Secondary Index (GSI) on Email. You want to efficiently find users by their email address. Which approach best uses the GSI to achieve this?
hard
A. Query the table using the GSI with KeyConditionExpression 'Email = :emailValue'
B. Scan the entire table filtering items where Email equals the value
C. Query the table using the primary key UserId with the email value
D. Create a new table with Email as the primary key
Solution
Step 1: Identify the best way to find users by Email
The GSI on Email allows querying by Email efficiently without scanning.
Step 2: Compare options
Query the table using the GSI with KeyConditionExpression 'Email = :emailValue' uses the GSI query correctly. Scan the entire table filtering items where Email equals the value scans, which is slow. Query the table using the primary key UserId with the email value uses UserId, which won't help find by Email. Create a new table with Email as the primary key is unnecessary duplication.
Final Answer:
Query the table using the GSI with KeyConditionExpression 'Email = :emailValue' -> Option A
Quick Check:
Use GSI query for efficient Email lookup [OK]
Hint: Use GSI query, not scan or primary key, for alternate keys [OK]
Common Mistakes:
Using Scan instead of Query on GSI
Querying primary key with wrong attribute
Creating redundant tables instead of indexes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.