LSI vs GSI comparison in DynamoDB - Performance Comparison
When using DynamoDB, we often add indexes to find data faster. Local Secondary Indexes (LSI) and Global Secondary Indexes (GSI) help with this.
We want to understand how the time to get data changes as the data grows when using LSI or GSI.
Analyze the time complexity of querying data using LSI and GSI.
// Query using LSI
const paramsLSI = {
TableName: "Orders",
IndexName: "OrderDateIndex", // LSI
KeyConditionExpression: "CustomerId = :cid",
ExpressionAttributeValues: { ":cid": { S: "123" } }
};
// Query using GSI
const paramsGSI = {
TableName: "Orders",
IndexName: "StatusIndex", // GSI
KeyConditionExpression: "OrderStatus = :status",
ExpressionAttributeValues: { ":status": { S: "SHIPPED" } }
};
This code queries orders by customer using an LSI and by status using a GSI.
Both queries scan matching items in the index to return results.
- Primary operation: Scanning index entries matching the query condition.
- How many times: Once per matching item in the index.
As the number of matching items grows, the query reads more entries.
| Input Size (matching items) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The work grows directly with how many items match the query.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of matching items.
[X] Wrong: "LSI queries are always faster than GSI queries because they are local."
[OK] Correct: Both LSI and GSI queries scan matching items, so speed depends on how many items match, not just index type.
Understanding how query time grows with data size helps you design better DynamoDB tables and indexes, a key skill for real projects.
What if we added a filter after the query to reduce results? How would that affect the time complexity?
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
