DeleteItem in DynamoDB - Time & Space Complexity
When we delete an item from a DynamoDB table, we want to know how the time it takes changes as the table grows.
We ask: How does deleting one item get slower or stay the same when the table has more data?
Analyze the time complexity of the following code snippet.
const params = {
TableName: "Users",
Key: { "UserId": { S: "123" } }
};
dynamodb.deleteItem(params, (err, data) => {
if (err) console.log(err);
else console.log("Item deleted");
});
This code deletes one item identified by its key from the "Users" table.
- Primary operation: DynamoDB looks up the item by its key and deletes it.
- How many times: This happens once per delete request.
Deleting one item by key does not require scanning the whole table.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 lookup and delete |
| 100 | 1 lookup and delete |
| 1000 | 1 lookup and delete |
Pattern observation: The time stays about the same no matter how many items are in the table.
Time Complexity: O(1)
This means deleting an item by its key takes about the same time, no matter how big the table is.
[X] Wrong: "Deleting an item gets slower as the table grows because it has to check every item."
[OK] Correct: DynamoDB uses the item's key to find it directly, so it does not scan the whole table.
Understanding how key-based operations work helps you explain efficient data access in real projects.
"What if we tried to delete items without specifying the key? How would the time complexity change?"