0
0
DynamoDBquery~30 mins

Why secondary indexes enable flexible queries in DynamoDB - See It in Action

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use the query command with --index-name AuthorGenreIndex and the correct key condition expression to filter by Author and Genre.