0
0
DynamoDBquery~3 mins

LSI vs GSI comparison in DynamoDB - When to Use Which

Choose your learning style9 modes available
The Big Idea

What if you could find any data instantly without flipping through every page?

The Scenario

Imagine you have a huge notebook where you write down all your friends' contact details. Now, you want to quickly find friends by their city or by their favorite hobby. But your notebook only lets you look up friends by their name, so you have to flip through every page to find what you want.

The Problem

Flipping through every page is slow and tiring. You might miss some entries or make mistakes. If you want to find friends by different details, you have to create multiple notebooks or write everything multiple times, which wastes space and causes confusion.

The Solution

LSI (Local Secondary Index) and GSI (Global Secondary Index) are like special indexes in your notebook that let you quickly find friends by other details without flipping every page. LSI lets you search within the same main partition key, while GSI lets you search across all partitions freely. They save time and keep your data organized.

Before vs After
Before
Scan entire table to find items by attribute
// very slow and costly
After
Query using LSI or GSI
// fast and efficient lookup
What It Enables

With LSI and GSI, you can quickly find data using different keys without scanning everything, making your app faster and more responsive.

Real Life Example

In an online store, you can find orders by customer ID (main key), but also quickly find orders by order date or status using LSI or GSI, so customers get their info instantly.

Key Takeaways

Manual searching is slow and error-prone.

LSI and GSI create fast, alternate ways to find data.

LSI works within a partition; GSI works across all data.