Put, get, and query operations in AWS - Time & Space Complexity
We want to understand how the time to complete database operations changes as we work with more data.
Specifically, how do put, get, and query actions behave when the amount of data grows?
Analyze the time complexity of the following operation sequence.
// Put operation: add one item
await dynamoDb.put({ TableName: 'MyTable', Item: item }).promise();
// Get operation: retrieve one item by key
const result = await dynamoDb.get({ TableName: 'MyTable', Key: key }).promise();
// Query operation: find items with a specific attribute
const results = await dynamoDb.query({
TableName: 'MyTable',
KeyConditionExpression: 'PartitionKey = :pk',
ExpressionAttributeValues: { ':pk': partitionKey }
}).promise();
This sequence shows adding one item, retrieving one item by key, and querying multiple items by a key.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Each put, get, or query call to DynamoDB.
- How many times: Each operation is called once per item or query request.
As the number of items grows, putting or getting one item stays the same time, but querying can take longer depending on how many items match.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | Put: 1, Get: 1, Query: up to 10 items returned |
| 100 | Put: 1, Get: 1, Query: up to 100 items returned |
| 1000 | Put: 1, Get: 1, Query: up to 1000 items returned |
Pattern observation: Put and get operations take constant time regardless of data size, but query time grows with the number of matching items.
Time Complexity: O(n)
This means put and get operations take the same time no matter how much data there is, but query operations take longer as more items match the query.
[X] Wrong: "All database operations take the same time no matter how much data exists."
[OK] Correct: Put and get are fast because they use keys, but query scans matching items and can take longer if many items match.
Understanding how different database operations scale helps you design efficient systems and answer questions confidently in real-world discussions.
"What if we added a filter expression to the query? How would the time complexity change?"