Tables, items, and attributes in AWS - Time & Space Complexity
When working with tables, items, and attributes in AWS databases, it is important to understand how the time to perform operations changes as the data grows.
We want to know how the number of operations or API calls changes when we add more items or attributes.
Analyze the time complexity of the following operation sequence.
// Create a table
aws dynamodb create-table --table-name MyTable --attribute-definitions AttributeName=Id,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
// Put multiple items
for i in range(1, n+1):
aws dynamodb put-item --table-name MyTable --item '{"Id": {"S": "item" + str(i)}, "Value": {"S": "data"}}'
// Get an item by key
aws dynamodb get-item --table-name MyTable --key '{"Id": {"S": "item42"}}'
This sequence creates a table, adds multiple items, and retrieves one item by its key.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Putting items into the table (put-item API call)
- How many times: Once per item, so n times for n items
- Getting an item by key happens once, so less impact on growth
As the number of items (n) increases, the number of put-item calls grows directly with n.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 put-item calls + 1 get-item call = 11 |
| 100 | 100 put-item calls + 1 get-item call = 101 |
| 1000 | 1000 put-item calls + 1 get-item call = 1001 |
Pattern observation: The total operations grow roughly in a straight line with the number of items added.
Time Complexity: O(n)
This means the time or number of operations grows directly in proportion to the number of items you add.
[X] Wrong: "Getting an item by key takes longer as the table grows because it searches all items."
[OK] Correct: DynamoDB uses keys to find items quickly, so getting an item by key takes about the same time no matter how many items are in the table.
Understanding how operations grow with data size helps you design efficient cloud databases and answer questions confidently in interviews.
"What if we changed from putting items one by one to batch writing multiple items at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of a table in DynamoDB
A table is a container that holds items, which are like rows in a database.Step 2: Identify the correct purpose
Since tables store items (records), this matches To store items, which are records.Final Answer:
To store items, which are records -> Option AQuick Check:
Table = container for items [OK]
- Confusing tables with code functions
- Thinking tables manage permissions
- Assuming tables host websites
Solution
Step 1: Recognize the attribute format
Attributes are key-value pairs, best represented as JSON objects with keys and values.Step 2: Match the correct syntax
{"Name": "John", "Age": 30}shows a JSON object with keys "Name" and "Age" and their values, which is correct.Final Answer:
{"Name": "John", "Age": 30} -> Option BQuick Check:
Attributes = key-value pairs in JSON [OK]
- Using arrays instead of key-value pairs
- Using tuples instead of objects
- Omitting braces for JSON objects
"UserID", what happens if you try to add two items with the same UserID but different attributes?Solution
Step 1: Understand primary key uniqueness
In DynamoDB, the primary key must be unique for each item in the table.Step 2: Effect of duplicate primary key
If you add an item with an existing primary key, it replaces the old item with the new one.Final Answer:
The second item overwrites the first item -> Option DQuick Check:
Duplicate key = overwrite existing item [OK]
- Thinking both items are stored separately
- Expecting an error on duplicate keys
- Assuming duplicates are allowed
Solution
Step 1: Recall primary key requirement
Every item must have the primary key attribute to be uniquely identified in the table.Step 2: Identify cause of error
If the item lacks the primary key attribute, DynamoDB rejects it with an error.Final Answer:
Missing required primary key attribute in the item -> Option AQuick Check:
Primary key missing causes insert error [OK]
- Assuming table capacity causes insert errors
- Blaming attribute data types without checking keys
- Thinking attribute count limits cause this error
UserID and may have multiple email addresses. How should you structure the table and items to handle this?Solution
Step 1: Identify unique identifier
UserID uniquely identifies each user, so it should be the primary key.Step 2: Store multiple emails
Since a user can have multiple emails, store them as a list attribute inside the user item.Step 3: Evaluate other options
Creating separate items per email duplicates UserID or loses uniqueness; using email as key ignores UserID; multiple tables is inefficient.Final Answer:
Use UserID as the primary key and store emails as a list attribute inside each item -> Option CQuick Check:
Primary key + list attribute for multiple values [OK]
- Using email as primary key instead of UserID
- Creating multiple items for one user
- Making multiple tables unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
