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?"
Practice
put operation do in AWS DynamoDB?Solution
Step 1: Understand the purpose of
Theputputoperation is used to save a new item or update an existing item in a DynamoDB table.Step 2: Compare with other operations
getretrieves one item by key,querysearches by key conditions, anddeleteremoves an item.Final Answer:
Saves or updates an item in the table -> Option AQuick Check:
Put = Save or update [OK]
- Confusing put with get or query
- Thinking put deletes data
- Assuming put only adds new items
Solution
Step 1: Identify the correct method for getting an item
The AWS SDK for JavaScript v3 usesgetmethod on the DynamoDB DocumentClient to retrieve a single item by key.Step 2: Check the parameters
The parameters includeTableNameandKeywith the key attribute and value.Final Answer:
dynamoDBClient.get({ TableName: 'MyTable', Key: { id: '123' } }) -> Option BQuick Check:
Get item uses get() with TableName and Key [OK]
- Using getItem() instead of get()
- Confusing query with get
- Using put() to get data
userId 'user1' and 'user2' only?const params = {
TableName: 'Users',
KeyConditionExpression: 'userId = :uid',
ExpressionAttributeValues: { ':uid': 'user1' }
};
const result = await dynamoDBClient.query(params);
console.log(result.Items);Solution
Step 1: Understand the query parameters
The query searches for items whereuserIdequals 'user1'.Step 2: Match items in the table
Since the table has 'user1' and 'user2', only the item with 'user1' matches and is returned.Final Answer:
[{ userId: 'user1', name: 'Alice' }] -> Option CQuick Check:
Query filters by key condition, returns matching items [OK]
- Expecting all items returned
- Confusing query with scan
- Thinking query returns error without full key
const params = {
TableName: 'Products',
Key: { productId: 101 }
};
const data = await dynamoDBClient.getItem(params);
console.log(data.Item);What is the likely cause of the error?
Solution
Step 1: Check the key attribute type
DynamoDB keys must be typed as attribute values, usually strings or objects with type wrappers.Step 2: Identify the error cause
Passing a plain number (101) without type wrapper causes a validation error.Final Answer:
The key attribute value must be a string, not a number -> Option AQuick Check:
Key values need correct type format [OK]
- Using wrong method name
- Missing KeyConditionExpression in getItem
- Incorrect table name
Solution
Step 1: Understand conditional update
To update only if the user exists, useputwith a condition expression that checks for the user's existence.Step 2: Use attribute_exists condition
The conditionattribute_exists(userId)ensures the item exists before updating, preventing overwriting non-existing items.Final Answer:
Useputwith a condition expression checking attribute_exists(userId) -> Option DQuick Check:
Conditional put prevents overwriting missing items [OK]
- Using get then put causes race conditions
- Using query unnecessarily for single item
- Putting without condition overwrites blindly
Ready to prove it?
Pass the test and this pattern moves unknown
