Querying a Global Secondary Index (GSI) in DynamoDB
📖 Scenario: You are managing a DynamoDB table that stores information about books in a library. Each book has a unique BookID as the primary key. You want to quickly find books by their Author using a Global Secondary Index (GSI).
🎯 Goal: Build a DynamoDB query to retrieve all books by a specific author using the GSI named AuthorIndex.
📋 What You'll Learn
Create a DynamoDB table named Books with BookID as the primary key.
Create a GSI named AuthorIndex with Author as the partition key.
Write a query to find all books by the author 'J.K. Rowling' using the GSI.
Use the AWS SDK for Python (boto3) syntax for the query.
💡 Why This Matters
🌍 Real World
Many applications need to query data by attributes other than the primary key. GSIs let you do this efficiently in DynamoDB.
💼 Career
Understanding how to create and query GSIs is essential for backend developers and database engineers working with DynamoDB.
Progress0 / 4 steps
1
Create the DynamoDB table data structure
Create a dictionary called table_definition that defines a DynamoDB table named Books with BookID as the primary key of type String. Include an attribute definition for BookID of type S.
DynamoDB
Hint
Define table_definition as a dictionary with keys TableName, AttributeDefinitions, and KeySchema. Use BookID as the partition key.
2
Add the Global Secondary Index (GSI) configuration
Add a GlobalSecondaryIndexes key to the table_definition dictionary. Define one GSI named AuthorIndex with Author as the partition key of type S. Also add Author to the AttributeDefinitions list.
DynamoDB
Hint
Update table_definition to include GlobalSecondaryIndexes with the correct index name and key schema. Also add Author to AttributeDefinitions.
3
Write the query to find books by author
Create a dictionary called query_params to query the Books table using the AuthorIndex. Set the IndexName to 'AuthorIndex', the KeyConditionExpression to check Author equals 'J.K. Rowling', and use the expression attribute values accordingly.
DynamoDB
Hint
Use boto3.dynamodb.conditions.Key to build the KeyConditionExpression. Set IndexName to 'AuthorIndex' and TableName to 'Books'.
4
Complete the query call to DynamoDB
Write a line of code that calls dynamodb_client.query() with query_params and stores the result in a variable called response. Assume dynamodb_client is already created.
DynamoDB
Hint
Use dynamodb_client.query(**query_params) to perform the query and assign the result to response.
Practice
(1/5)
1. What is the main purpose of querying a Global Secondary Index (GSI) in DynamoDB?
easy
A. To delete items from the table
B. To create a backup of the table
C. To update the primary key of items
D. To find items using an alternate key without scanning the whole table
Solution
Step 1: Understand what a GSI is used for
A GSI allows querying data using a different key than the primary key.
Step 2: Identify the main benefit of querying a GSI
Querying a GSI helps find items quickly without scanning the entire table.
Final Answer:
To find items using an alternate key without scanning the whole table -> Option D
Quick Check:
Query GSI = alternate key search [OK]
Hint: GSI lets you search by different keys fast [OK]
Common Mistakes:
Confusing GSI query with table scan
Thinking GSI is for deleting or updating keys
Assuming GSI creates backups
2. Which of the following is the correct way to specify a GSI name in a DynamoDB query using AWS SDK?
B. The attribute 'category' is not the partition key of the GSI 'CategoryIndex'
C. ExpressionAttributeValues is missing a value
D. The table name 'Products' is incorrect
Solution
Step 1: Understand GSI key requirements
When querying a GSI, the KeyConditionExpression must use the GSI's partition key attribute.
Step 2: Identify the error cause
If 'category' is not the partition key of 'CategoryIndex', the query will fail with an error.
Final Answer:
The attribute 'category' is not the partition key of the GSI 'CategoryIndex' -> Option B
Quick Check:
GSI query key must be GSI partition key [OK]
Hint: GSI query key must match GSI partition key [OK]
Common Mistakes:
Using a non-key attribute in KeyConditionExpression
Assuming table name typo causes this error
Believing GSI queries are unsupported
5. You have a DynamoDB table 'Employees' with a GSI named 'DeptIndex' where the partition key is 'department' and sort key is 'hireDate'. You want to find all employees in 'Sales' hired after '2023-01-01'. Which query is correct?