Creating a Global Secondary Index (GSI) in DynamoDB
📖 Scenario: You are managing a DynamoDB table for an online bookstore. The table stores information about books, including their BookID, Title, Author, and Genre. You want to create a Global Secondary Index (GSI) to efficiently query books by their Author.
🎯 Goal: Build a DynamoDB table schema with a Global Secondary Index (GSI) named AuthorIndex that uses Author as the partition key. This will allow quick lookups of books by author.
📋 What You'll Learn
Create a DynamoDB table named Books with BookID as the primary partition key.
Add a Global Secondary Index (GSI) named AuthorIndex with Author as the partition key.
Set the projection type of the GSI to ALL to include all attributes.
Specify the provisioned throughput for both the table and the GSI.
💡 Why This Matters
🌍 Real World
Global Secondary Indexes let you query DynamoDB tables efficiently using different keys than the primary key. This is common in real-world apps needing flexible search options.
💼 Career
Understanding how to create and configure GSIs is essential for backend developers and database administrators working with DynamoDB to optimize data access patterns.
Progress0 / 4 steps
1
Create the DynamoDB table schema
Create a DynamoDB table schema named Books with BookID as the primary partition key of type S (string). Define the attribute definitions for BookID, Title, Author, and Genre all as type S.
DynamoDB
Hint
Define the table name, attribute definitions for all attributes as strings, and set BookID as the partition key in the key schema.
2
Add Global Secondary Index (GSI) configuration
Add a GlobalSecondaryIndexes section to the table schema. Create a GSI named AuthorIndex with Author as the partition key (HASH). Set the projection type to ALL and provisioned throughput to 5 read and 5 write capacity units.
DynamoDB
Hint
Inside GlobalSecondaryIndexes, define the index name, key schema with Author as partition key, projection type as ALL, and provisioned throughput.
3
Verify attribute definitions include GSI key
Ensure the AttributeDefinitions section includes the Author attribute with type S because it is used as the partition key in the GSI.
DynamoDB
Hint
Make sure Author is listed in AttributeDefinitions with type S because it is the GSI partition key.
4
Complete the table creation JSON with all parts
Combine all parts to form the complete DynamoDB table creation JSON. It should include TableName, AttributeDefinitions, KeySchema, ProvisionedThroughput, and GlobalSecondaryIndexes with the AuthorIndex GSI.
DynamoDB
Hint
Make sure all parts are included together in one JSON object to create the table with the GSI.
Practice
(1/5)
1. What is the main purpose of creating a Global Secondary Index (GSI) in DynamoDB?
easy
A. To automatically backup the table data
B. To increase the storage capacity of the table
C. To allow querying the table using alternative key attributes
D. To encrypt the table data at rest
Solution
Step 1: Understand what a GSI does
A GSI lets you query a DynamoDB table using different key attributes than the primary key.
Step 2: Compare options to GSI purpose
Options A, B, and D describe backup, storage, and encryption, which are unrelated to GSI functionality.
Final Answer:
To allow querying the table using alternative key attributes -> Option C
Quick Check:
GSI = alternative query keys [OK]
Hint: GSIs let you query by new keys without changing the table [OK]
Common Mistakes:
Thinking GSI increases storage size
Confusing GSI with backup or encryption features
Assuming GSI changes the primary key
2. Which of the following is the correct syntax snippet to define a GSI named OrderDateIndex with OrderDate as the partition key in a DynamoDB table creation JSON?
B. NonKeyAttributes must be a non-empty array when ProjectionType is INCLUDE
C. KeyType must be RANGE for GSIs
D. ProjectionType cannot be INCLUDE for GSIs
Solution
Step 1: Check ProjectionType and NonKeyAttributes
When ProjectionType is INCLUDE, NonKeyAttributes must be a non-empty array listing attributes to project.
Step 2: Verify given snippet
NonKeyAttributes is [], which is empty and invalid when ProjectionType is INCLUDE.
Step 3: Eliminate other options
ProjectionType INCLUDE is valid; KeyType HASH is valid for partition key; IndexName can contain 'Index'.
Final Answer:
NonKeyAttributes must be a non-empty array when ProjectionType is INCLUDE -> Option B
Quick Check:
INCLUDE projection requires NonKeyAttributes [OK]
Hint: INCLUDE projection needs NonKeyAttributes array [OK]
Common Mistakes:
Using empty or missing NonKeyAttributes with INCLUDE
Confusing KeyType HASH and RANGE for partition key
Thinking IndexName naming causes errors
5. You have a DynamoDB table with primary key UserID. You want to query users by their City and SignupDate. Which GSI definition allows efficient queries for all users in a city sorted by signup date?