0
0
AWScloud~5 mins

Put, get, and query operations in AWS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Put, get, and query operations
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
10Put: 1, Get: 1, Query: up to 10 items returned
100Put: 1, Get: 1, Query: up to 100 items returned
1000Put: 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.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how different database operations scale helps you design efficient systems and answer questions confidently in real-world discussions.

Self-Check

"What if we added a filter expression to the query? How would the time complexity change?"