0
0
DynamoDBquery~30 mins

LSI vs GSI comparison in DynamoDB - Hands-On Comparison

Choose your learning style9 modes available
Comparing Local Secondary Index (LSI) and Global Secondary Index (GSI) in DynamoDB
📖 Scenario: You are building a simple DynamoDB table to store information about books in a library. You want to learn how to use Local Secondary Indexes (LSI) and Global Secondary Indexes (GSI) to query the data in different ways.
🎯 Goal: Create a DynamoDB table with a primary key and add one Local Secondary Index (LSI) and one Global Secondary Index (GSI). Understand how to define them and see the difference in their setup.
📋 What You'll Learn
Create a DynamoDB table named Books with Author as the partition key and Title as the sort key.
Add a Local Secondary Index (LSI) named GenreIndex with Author as the partition key and Genre as the sort key.
Add a Global Secondary Index (GSI) named PublisherIndex with Publisher as the partition key and Year as the sort key.
Use the AWS CLI JSON format for the table creation and index definitions.
💡 Why This Matters
🌍 Real World
In real-world applications, DynamoDB indexes help you query data efficiently in different ways without scanning the entire table.
💼 Career
Understanding LSIs and GSIs is important for database developers and cloud engineers working with AWS DynamoDB to optimize data access patterns.
Progress0 / 4 steps
1
Create the DynamoDB table with primary key
Create a DynamoDB table named Books with Author as the partition key (type String) and Title as the sort key (type String). Use the AWS CLI JSON format for the table definition and assign it to a variable called table_definition.
DynamoDB
Need a hint?

Remember, the partition key is called Author and the sort key is called Title. Both are strings.

2
Add a Local Secondary Index (LSI)
Add a Local Secondary Index (LSI) named GenreIndex to the table_definition. The LSI should use Author as the partition key and Genre as the sort key (type String). Update the AttributeDefinitions to include Genre. Add the LSI inside the LocalSecondaryIndexes list.
DynamoDB
Need a hint?

LSI uses the same partition key Author but a different sort key Genre. Add Genre to AttributeDefinitions.

3
Add a Global Secondary Index (GSI)
Add a Global Secondary Index (GSI) named PublisherIndex to the table_definition. The GSI should use Publisher as the partition key and Year as the sort key (both type String). Update the AttributeDefinitions to include Publisher and Year. Add the GSI inside the GlobalSecondaryIndexes list with provisioned throughput of 5 read and 5 write units.
DynamoDB
Need a hint?

GSI uses different partition and sort keys: Publisher and Year. Add both to AttributeDefinitions and define the GSI with provisioned throughput.

4
Complete the table definition with all indexes
Ensure the table_definition includes the primary key, the Local Secondary Index GenreIndex, and the Global Secondary Index PublisherIndex with all required attribute definitions and provisioned throughput settings.
DynamoDB
Need a hint?

Check that all parts are included: primary key, LSI, GSI, attribute definitions, and throughput.