Multiple actions in one update in DynamoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When updating a DynamoDB item with several changes at once, it's important to know how the work grows as the number of changes increases.
We want to understand how the time to update changes when we add more actions in a single update.
Analyze the time complexity of the following code snippet.
UpdateItem {
Key: { id: "123" },
UpdateExpression: "SET attr1 = :val1, attr2 = :val2, attr3 = :val3",
ExpressionAttributeValues: {
":val1": "value1",
":val2": "value2",
":val3": "value3"
}
}
This code updates one item by setting three attributes in a single update request.
- Primary operation: Applying each update action to the item attributes.
- How many times: Once for each attribute being updated (3 times in this example).
Each additional attribute update adds a small extra step to the work done.
| Input Size (number of updates) | Approx. Operations |
|---|---|
| 3 | 3 update actions |
| 10 | 10 update actions |
| 100 | 100 update actions |
Pattern observation: The work grows directly with the number of update actions.
Time Complexity: O(n)
This means the time to complete the update grows in a straight line as you add more actions.
[X] Wrong: "Doing multiple updates in one request takes the same time as one update."
[OK] Correct: Each update action adds work, so more actions mean more time, not the same time.
Understanding how multiple updates affect time helps you explain and predict performance in real DynamoDB tasks.
"What if we batch multiple update requests instead of combining actions in one update? How would the time complexity change?"
Practice
Solution
Step 1: Understand update actions in DynamoDB
DynamoDB update expressions use SET to add or modify attributes in an item.Step 2: Identify the correct action for adding or changing attributes
SET is used to add new attributes or change existing ones in a single update request.Final Answer:
SET -> Option BQuick Check:
SET updates attributes [OK]
- Confusing REMOVE with adding attributes
- Using ADD to change non-numeric attributes
- Thinking DELETE removes attributes instead of set elements
score by adding 10 in a DynamoDB update expression?Solution
Step 1: Recall syntax for numeric increments in DynamoDB
To increase a numeric attribute, DynamoDB uses ADD followed by the attribute and the value.Step 2: Match the correct syntax
ADD score 10 correctly adds 10 to the existing score attribute.Final Answer:
ADD score 10 -> Option DQuick Check:
ADD increments numbers [OK]
- Using SET with arithmetic expressions (not supported)
- Using REMOVE or DELETE for numeric increments
- Incorrect syntax like missing commas or keywords
SET age = age + :inc REMOVE nickname ADD points :pts DELETE tags :oldTagsWhat will happen if the item initially has
age = 30, nickname = 'Sam', points = 5, and tags = {'red', 'blue'} with :inc = 2, :pts = 3, and :oldTags = {'blue'}?Solution
Step 1: Apply each update action to the initial item
SET age = age + :inc adds 2 to 30 -> 32; REMOVE nickname deletes 'Sam'; ADD points :pts adds 3 to 5 -> 8; DELETE tags :oldTags removes 'blue' from {'red', 'blue'} -> {'red'}.Step 2: Confirm final attribute values
age=32, nickname removed, points=8, tags={'red'} matches age becomes 32, nickname removed, points become 8, tags become {'red'}.Final Answer:
age becomes 32, nickname removed, points become 8, tags become {'red'} -> Option CQuick Check:
Multiple actions update all attributes correctly [OK]
- Forgetting REMOVE deletes attribute
- Confusing DELETE with REMOVE for sets
- Misapplying ADD to sets instead of numbers
SET name = :n ADD age :a REMOVE points, DELETE tags :tSolution
Step 1: Check syntax for multiple update actions
REMOVE action lists attributes separated by spaces, not commas. The comma after points is invalid.Step 2: Validate other parts
ADD can be used with numeric attributes; SET can use placeholders; REMOVE and DELETE can coexist.Final Answer:
Comma after REMOVE points is invalid syntax -> Option AQuick Check:
REMOVE uses spaces, not commas [OK]
- Using commas in REMOVE or DELETE lists
- Assuming ADD works on non-numeric attributes
- Thinking placeholders are not allowed in SET
- Increase
stock by 5- Remove attribute
discontinued- Add
newTag to a set attribute tagsWhich update expression correctly performs all these actions?
Solution
Step 1: Choose correct action for increasing numeric attribute
ADD stock :inc correctly increases numeric stock by :inc.Step 2: Remove attribute and add to set correctly
REMOVE discontinued deletes the attribute; ADD tags :newTag adds elements to the set.Step 3: Verify syntax correctness
ADD stock :inc REMOVE discontinued ADD tags :newTag uses ADD for numeric and set additions, REMOVE for attribute removal, all valid.Final Answer:
ADD stock :inc REMOVE discontinued ADD tags :newTag -> Option AQuick Check:
Use ADD for numbers and sets, REMOVE for attributes [OK]
- Using SET with arithmetic expressions (not supported)
- Using DELETE instead of REMOVE for attributes
- Trying to add to sets with SET instead of ADD
