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.
Practice
Solution
Step 1: Analyze LSI key schema
LSI shares the same partition key as the base table but allows a different sort key for alternate sorting within that partition.Step 2: Analyze GSI key schema
GSI allows both partition key and sort key to be different from the base table, enabling flexible query patterns across partitions.Final Answer:
LSI uses the same partition key but a different sort key; GSI uses different partition and sort keys. -> Option AQuick Check:
LSI = same partition key, different sort key; GSI = different keys [OK]
- Confusing which keys LSI and GSI use
- Thinking LSI can have different partition keys
- Assuming GSI uses the same keys as the base table
Solution
Step 1: Check key schema order for GSI
GSI requires a partition key (HASH) and optionally a sort key (RANGE). The partition key must come first in the KeySchema array.Step 2: Validate attribute roles and projection
{ "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "UserId", "KeyType": "HASH"}, {"AttributeName": "OrderDate", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "ALL"} } correctly defines UserId as HASH and OrderDate as RANGE with ProjectionType ALL, which is valid for GSI.Final Answer:
{\n \"IndexName\": \"GSI1\",\n \"KeySchema\": [\n {\"AttributeName\": \"UserId\", \"KeyType\": \"HASH\"},\n {\"AttributeName\": \"OrderDate\", \"KeyType\": \"RANGE\"}\n ],\n \"Projection\": {\"ProjectionType\": \"ALL\"}\n} -> Option BQuick Check:
GSI KeySchema starts with HASH key [OK]
- Swapping HASH and RANGE key types
- Using LSI name for GSI definition
- Missing required HASH key in KeySchema
Query on LSI where UserId = '123' and OrderStatus = 'Pending'
Solution
Step 1: Recall LSI query keys
LSI uses the same partition key as the base table but allows querying by the alternate sort key, here 'OrderStatus'.Step 2: Analyze query conditions
The query filters on UserId = '123' (partition key) and OrderStatus = 'Pending' (LSI sort key), so it returns matching items sorted by OrderStatus.Final Answer:
Returns all items with UserId '123' and OrderStatus 'Pending' sorted by OrderStatus. -> Option DQuick Check:
LSI query uses same partition key + LSI sort key [OK]
- Confusing sort key names between base and LSI
- Assuming LSI cannot be queried on alternate sort key
- Thinking query returns all UserId items ignoring sort key
Solution
Step 1: Check attribute presence for GSI indexing
GSI indexes only items that have the specified partition and sort key attributes. Missing attributes mean no index entry.Step 2: Understand query behavior on GSI
If items lack 'Category' or 'Price', they won't appear in the GSI, causing queries to return no results despite existing base table items.Final Answer:
The GSI was not properly populated because the base table items lack the 'Category' or 'Price' attributes. -> Option AQuick Check:
Missing GSI keys in items = no query results [OK]
- Assuming GSI queries can't filter on sort key
- Confusing LSI and GSI usage
- Thinking base table keys must be in GSI query
Solution
Step 1: Base table supports UserId-Timestamp query
Queries by 'UserId' sorted by 'Timestamp' use base table keys directly.Step 2: GSI supports Status-OrderDate query
Queries by 'Status' sorted by 'OrderDate' require a different partition key ('Status'), so a GSI with partition key 'Status' and sort key 'OrderDate' is needed.Final Answer:
Use the base table and a GSI with partition key 'Status' and sort key 'OrderDate'. -> Option CQuick Check:
LSI = same partition key, GSI = different partition key [OK]
- Trying to use LSI with different partition key
- Using only base table keys for all queries
- Confusing which index supports which query pattern
Ready to prove it?
Pass the test and this pattern moves unknown
