Local Secondary Index (LSI) concept in DynamoDB - Time & Space Complexity
When using a Local Secondary Index (LSI) in DynamoDB, it's important to understand how the time to get your data changes as your data grows.
We want to know how the number of operations grows when we query using an LSI.
Analyze the time complexity of this DynamoDB query using an LSI.
const params = {
TableName: "Orders",
IndexName: "CustomerDateIndex", // LSI on CustomerID and OrderDate
KeyConditionExpression: "CustomerID = :cid",
ExpressionAttributeValues: {
":cid": { S: "12345" }
}
};
const result = await dynamodb.query(params).promise();
This code queries the Orders table using an LSI to find all orders for a specific customer.
Look at what repeats when the query runs.
- Primary operation: Scanning the items with the same partition key (CustomerID) in the LSI.
- How many times: Once per matching item for that customer.
As the number of orders for a customer grows, the query checks more items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 orders | About 10 item checks |
| 100 orders | About 100 item checks |
| 1000 orders | About 1000 item checks |
Pattern observation: The work grows directly with the number of matching items.
Time Complexity: O(n)
This means the time to get results grows linearly with how many items match the query.
[X] Wrong: "Querying with an LSI always takes the same time no matter how many items match."
[OK] Correct: The query must check each matching item, so more items mean more work and longer time.
Understanding how queries scale with data size shows you know how to design efficient data access in DynamoDB.
What if we changed the query to use a Global Secondary Index (GSI) instead of an LSI? How would the time complexity change?
Practice
Solution
Step 1: Understand LSI functionality
LSI lets you query items with the same partition key but sorted by an alternate sort key.Step 2: Compare options
Only To allow querying data with the same partition key but different sort keys correctly describes this feature; others describe unrelated features.Final Answer:
To allow querying data with the same partition key but different sort keys -> Option BQuick Check:
LSI = alternate sort key with same partition key [OK]
- Thinking LSI creates new partition keys
- Confusing LSI with Global Secondary Index
- Believing LSI stores data separately
Solution
Step 1: Recall LSI definition rules
LSIs must use the same partition key as the main table but can have a different sort key.Step 2: Check options
Only Define the LSI with the same partition key but a different sort key matches this rule; others violate partition key or timing constraints.Final Answer:
Define the LSI with the same partition key but a different sort key -> Option DQuick Check:
LSI = same partition key + different sort key [OK]
- Trying to create LSI after table creation
- Using different partition keys for LSI
- Omitting sort key in LSI definition
UserID and sort key Timestamp, and an LSI with sort key OrderStatus, what will the following query return?Query: KeyConditionExpression = UserID = '123' AND OrderStatus = 'Pending'Solution
Step 1: Understand query with LSI
Query on LSI uses same partition key but different sort key (OrderStatus). The query filters UserID = '123' and OrderStatus = 'Pending'.Step 2: Analyze query result
It returns items with UserID '123' and OrderStatus 'Pending', sorted by OrderStatus.Final Answer:
All items with UserID '123' and OrderStatus 'Pending' sorted by OrderStatus -> Option CQuick Check:
Query on LSI filters by partition and LSI sort key [OK]
- Assuming LSI changes partition key
- Thinking query returns all items with OrderStatus only
- Believing syntax error occurs using LSI sort key
Solution
Step 1: Check LSI query requirements
Queries on LSI require the correct partition key value; wrong partition key returns no results.Step 2: Evaluate options
The partition key value in the query is incorrect explains no results due to incorrect partition key; other options are less likely or invalid.Final Answer:
The partition key value in the query is incorrect -> Option AQuick Check:
Correct partition key needed for LSI query [OK]
- Trying to add LSI after table creation
- Ignoring missing sort key attributes
- Confusing LSI partition key with main table partition key
CustomerID and sort key OrderDate. You want to query all orders for a customer sorted by OrderStatus instead of OrderDate. How should you design the Local Secondary Index (LSI) to achieve this?Solution
Step 1: Understand LSI design rules
LSI must use the same partition key as the main table but can have a different sort key.Step 2: Apply to problem
To sort by OrderStatus for the same CustomerID, create an LSI with partition key CustomerID and sort key OrderStatus at table creation.Step 3: Eliminate wrong options
Create an LSI with partition keyOrderStatusand sort keyOrderDateafter table creation is invalid because LSI cannot be created after table creation; Create a Global Secondary Index (GSI) with partition keyCustomerIDand sort keyOrderStatusis about GSI, not LSI; AddOrderStatusas a new partition key in the main table changes partition key, which is not allowed.Final Answer:
Create an LSI with partition key CustomerID and sort key OrderStatus defined at table creation -> Option AQuick Check:
LSI = same partition key + new sort key at creation [OK]
- Trying to create LSI after table creation
- Confusing LSI with GSI
- Changing partition key in LSI
Ready to prove it?
Pass the test and this pattern moves unknown
