TransactGetItems in DynamoDB - Time & Space Complexity
When using TransactGetItems in DynamoDB, we want to know how the time to get data changes as we ask for more items.
We ask: How does the number of items affect the total time to get them all?
Analyze the time complexity of the following code snippet.
const params = {
TransactItems: [
{ Get: { TableName: "Users", Key: { UserId: "1" } } },
{ Get: { TableName: "Orders", Key: { OrderId: "101" } } },
{ Get: { TableName: "Products", Key: { ProductId: "500" } } }
]
};
const result = await dynamodb.transactGetItems(params).promise();
This code fetches multiple items from different tables in a single transaction.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each item requested in TransactItems is fetched once.
- How many times: The operation repeats once per item in the TransactItems array.
As you ask for more items, the total time grows roughly in direct proportion to the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 item fetches |
| 100 | About 100 item fetches |
| 1000 | About 1000 item fetches |
Pattern observation: The time grows steadily as you add more items, like counting one by one.
Time Complexity: O(n)
This means the time to complete the transaction grows linearly with the number of items requested.
[X] Wrong: "TransactGetItems fetches all items instantly, no matter how many."
[OK] Correct: Each item still requires a fetch operation, so more items mean more work and more time.
Understanding how batch operations scale helps you design efficient data access and shows you grasp how cloud databases work under the hood.
"What if we changed TransactGetItems to GetItem calls one by one? How would the time complexity change?"