Sparse index pattern in DynamoDB - Time & Space Complexity
When using a sparse index in DynamoDB, we want to know how the time to find data changes as the data grows.
We ask: How does the search time grow when the table and index get bigger?
Analyze the time complexity of this sparse index query.
const params = {
TableName: "Orders",
IndexName: "StatusIndex", // sparse index on 'status'
KeyConditionExpression: "status = :s",
ExpressionAttributeValues: {
":s": "SHIPPED"
}
};
const result = await dynamodb.query(params).promise();
This code queries only items with status "SHIPPED" using a sparse index that stores only those items.
Look for repeated work inside the query.
- Primary operation: Scanning the sparse index entries matching the status.
- How many times: Once per matching item in the sparse index, not the whole table.
As the number of items with status "SHIPPED" grows, the query takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 shipped items | 10 reads |
| 100 shipped items | 100 reads |
| 1000 shipped items | 1000 reads |
Pattern observation: The time grows linearly with the number of matching items, not the total table size.
Time Complexity: O(m)
This means the query time grows in direct proportion to the number of items matching the sparse index condition.
[X] Wrong: "Querying a sparse index is as slow as scanning the whole table."
[OK] Correct: The sparse index only contains items with the indexed attribute, so the query touches fewer items, making it faster.
Understanding sparse indexes helps you explain how to efficiently query subsets of data, a useful skill in real projects.
"What if the sparse index included multiple attributes for filtering? How would that affect the time complexity?"
Practice
sparse index pattern in DynamoDB?Solution
Step 1: Understand sparse index purpose
Sparse index only includes items that have the indexed attribute, skipping others.Step 2: Identify benefit of selective indexing
This selective indexing reduces query cost and speeds up queries for that subset.Final Answer:
It indexes only items with a specific attribute, making queries faster and cheaper. -> Option BQuick Check:
Sparse index = selective fast queries [OK]
- Thinking sparse index includes all items
- Confusing sparse index with full table scan
- Assuming sparse index duplicates data
Solution
Step 1: Identify sparse index creation method
Sparse indexes use GSIs with keys only on items having the attribute.Step 2: Check options for correctness
Create a Global Secondary Index (GSI) with a partition key only on items having the attribute. matches this method; others either index all items or use invalid keys.Final Answer:
Create a Global Secondary Index (GSI) with a partition key only on items having the attribute. -> Option AQuick Check:
Sparse index = GSI with selective keys [OK]
- Confusing LSI with sparse index
- Using null keys in GSI partition key
- Duplicating primary key in GSI
status, and a sparse GSI on status, what will this query return?Query on GSI where status = 'active'Solution
Step 1: Understand sparse GSI query behavior
The sparse GSI includes only items with the status attribute, so querying status='active' returns those items.Step 2: Eliminate incorrect options
All items in the table regardless of status. is wrong because items without status are not in GSI; C is wrong because query targets status='active'; D is wrong because GSI keys can be queried.Final Answer:
All items in the table with status 'active'. -> Option CQuick Check:
Query sparse GSI = items with attribute value [OK]
- Expecting all table items in result
- Thinking query fails without primary key
- Confusing sparse index with full scan
category, but your query returns no results even though items have category. What is a likely cause?Solution
Step 1: Check GSI key configuration
If the GSI partition key is notcategory, the index won't include those items properly.Step 2: Evaluate other options
Table having no items is unlikely if items exist; querying base table returns items but not via GSI; sparse indexes do support queries.Final Answer:
The GSI partition key is not set tocategory. -> Option DQuick Check:
Wrong GSI key = no results [OK]
- Querying base table instead of GSI
- Assuming sparse index can't be queried
- Ignoring GSI key setup
deliveryDate attribute set, but most orders don't have this attribute. How can you use a sparse index pattern to optimize queries?Solution
Step 1: Use sparse index with deliveryDate attribute
Creating a GSI with deliveryDate as partition key includes only items having that attribute, making queries efficient.Step 2: Evaluate other options
A constant partition key approach requires adding an extra attribute to all items, incurring write costs; main table sort key doesn't filter missing attributes; full table scan is inefficient.Final Answer:
Create a GSI withdeliveryDateas the partition key; only orders with this attribute appear in the index. -> Option AQuick Check:
Sparse GSI on attribute = fast filtered queries [OK]
- Using scan instead of sparse index
- Indexing all items losing sparsity
- Misusing sort key for filtering
Ready to prove it?
Pass the test and this pattern moves unknown
