GSI overloading technique in DynamoDB - Time & Space Complexity
When using Global Secondary Indexes (GSIs) in DynamoDB, it's important to understand how query time grows as data increases.
We want to see how the GSI overloading technique affects the speed of queries as more items are stored.
Analyze the time complexity of this DynamoDB query using a GSI with overloading.
// Query using GSI with overloaded partition key
const params = {
TableName: "Orders",
IndexName: "GSI1",
KeyConditionExpression: "GSI1PK = :gsiKey",
ExpressionAttributeValues: {
":gsiKey": "USER#123"
}
};
const result = await dynamodb.query(params).promise();
This code queries the GSI where the partition key is overloaded to group multiple item types under one key.
Look for repeated work inside the query process.
- Primary operation: Scanning items with the same GSI partition key.
- How many times: Once per query, but the number of items returned depends on how many share the overloaded key.
As more items share the same overloaded GSI key, the query must process more data.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 items scanned |
| 100 | 100 items scanned |
| 1000 | 1000 items scanned |
Pattern observation: The work grows linearly with the number of items sharing the overloaded key.
Time Complexity: O(n)
This means the query time grows directly with the number of items that share the overloaded GSI key.
[X] Wrong: "Using a GSI with an overloaded key always makes queries fast regardless of data size."
[OK] Correct: When many items share the same overloaded key, the query must scan all of them, so time grows with that number.
Understanding how GSI overloading affects query time helps you design better DynamoDB tables and answer questions about scaling data access.
"What if we split the overloaded GSI key into multiple keys? How would that change the query time complexity?"
Practice
GSI overloading technique in DynamoDB?Solution
Step 1: Understand GSI overloading purpose
GSI overloading is designed to let one index serve many query types by using prefixes and composite keys.Step 2: Identify the main benefit
This reduces the number of GSIs needed, saving costs and improving query flexibility.Final Answer:
It allows using a single Global Secondary Index for multiple query patterns. -> Option AQuick Check:
GSI overloading = single index, multiple queries [OK]
- Thinking GSI overloading encrypts data
- Confusing GSI overloading with item size limits
- Assuming it enables multi-region replication
Solution
Step 1: Understand prefix usage in GSI keys
Prefixes like "USER#" help separate different entity types in the same GSI.Step 2: Check correct concatenation
The prefix should come first, then the unique identifier, so "USER#" + UserId is correct.Final Answer:
PartitionKey = "USER#" + UserId -> Option AQuick Check:
Prefix before ID = "USER#" + UserId [OK]
- Putting prefix after the ID
- Using no prefix at all
- Adding extra symbols incorrectly
PK and sort key is SK, what will be the result of this query?Query: PK = "ORDER#123" SK begins_with "ITEM#"
Solution
Step 1: Understand the query keys
The query filters on PK = "ORDER#123" and SK begins with "ITEM#" which targets items under order 123.Step 2: Interpret begins_with on SK
Using begins_with on SK returns all sort keys starting with "ITEM#", so all order items are returned.Final Answer:
Returns all items related to order 123 with SK starting with "ITEM#" -> Option DQuick Check:
PK exact + SK begins_with = filtered order items [OK]
- Thinking begins_with applies to PK
- Assuming SK must be exact match
- Believing SK cannot be filtered in query
GSI_PK and sort key GSI_SK. You want to query all "USER" entities but your query returns no results. Which of the following is the most likely cause?Solution
Step 1: Check prefix usage in GSI overloading
GSI overloading requires prefixes like "USER#" in the partition key to separate entity types.Step 2: Identify missing prefix impact
If the query omits the prefix, it won't match any items because keys are stored with prefixes.Final Answer:
You forgot to add the prefix "USER#" to the partition key value in the query. -> Option BQuick Check:
Missing prefix in PK = no results [OK]
- Using begins_with on partition key
- Querying base table instead of GSI
- Using filter instead of key condition
USER and ORDER entities in the same index. Which key design below best supports efficient queries for:1) All orders by a user
2) All users by status
Choose the best partition key and sort key combination.
Solution
Step 1: Analyze query requirements
Query 1 needs all orders by a user, so partition key should be "USER#" + UserId for orders.
Query 2 needs all users by status, so partition key should be "STATUS#" + Status for users.Step 2: Check sort key design
Sort keys differentiate entities: "ORDER#" + OrderId for orders, "USER#" + UserId for users to keep uniqueness and sorting.Step 3: Evaluate options
Partition Key: "USER#" + UserId for orders, "STATUS#" + Status for users; Sort Key: "ORDER#" + OrderId for orders, "USER#" + UserId for users matches these requirements by overloading the GSI with different prefixes for partition keys and sort keys.Final Answer:
Partition Key: "USER#" + UserId for orders, "STATUS#" + Status for users; Sort Key: "ORDER#" + OrderId for orders, "USER#" + UserId for users -> Option CQuick Check:
Use different prefixes per entity and query need [OK]
- Using same prefix for all entities
- Not differentiating partition keys by query type
- Using entity ID as partition key without prefix
Ready to prove it?
Pass the test and this pattern moves unknown
