What if you could find any data instantly without flipping through every page?
LSI vs GSI comparison in DynamoDB - When to Use Which
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.
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.
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.
Scan entire table to find items by attribute
// very slow and costlyQuery using LSI or GSI // fast and efficient lookup
With LSI and GSI, you can quickly find data using different keys without scanning everything, making your app faster and more responsive.
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.
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.