First table creation in DynamoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When creating a table in DynamoDB, it's important to understand how the time it takes grows as the table size or settings change.
We want to know how the work done by DynamoDB changes when we create a new table.
Analyze the time complexity of the following code snippet.
aws dynamodb create-table \
--table-name MusicCollection \
--attribute-definitions AttributeName=Artist,AttributeType=S \
--key-schema AttributeName=Artist,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
This code creates a new DynamoDB table named MusicCollection with a simple primary key and set throughput.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The creation process involves setting up metadata and allocating resources internally.
- How many times: This happens once per table creation; no loops or repeated scans over data occur here.
Creating a table takes roughly the same amount of work no matter how many items will be stored later.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Same small fixed setup work |
| 100 | Same small fixed setup work |
| 1000 | Same small fixed setup work |
Pattern observation: The time to create a table does not grow with the number of items because the table starts empty.
Time Complexity: O(1)
This means creating a table takes a fixed amount of time regardless of future data size.
[X] Wrong: "Creating a table takes longer if I plan to add many items later."
[OK] Correct: The table creation only sets up the structure, not the data, so it takes the same time no matter how many items you add later.
Understanding that table creation is a fixed cost helps you explain how databases prepare for data storage efficiently.
"What if we added a global secondary index during table creation? How would the time complexity change?"
Practice
Solution
Step 1: Understand DynamoDB table basics
Every DynamoDB table must have a unique name and a primary key to identify items.Step 2: Identify required elements for creation
Secondary indexes and throughput settings are optional or have defaults, but primary key and table name are mandatory.Final Answer:
Table name and primary key -> Option CQuick Check:
Minimum required = Table name + primary key [OK]
- Forgetting to define the primary key
- Confusing secondary index as mandatory
- Omitting the table name
Users with a primary key UserId of type string?Solution
Step 1: Check AWS CLI syntax for create-table
The correct command uses 'aws dynamodb create-table' with options: --table-name, --attribute-definitions, --key-schema, and --provisioned-throughput.Step 2: Verify option correctness
aws dynamodb create-table --table-name Users --attribute-definitions AttributeName=UserId,AttributeType=S --key-schema AttributeName=UserId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 correctly specifies attribute name and type, key schema with HASH key, and provisioned throughput. Others use invalid or incomplete syntax.Final Answer:
aws dynamodb create-table --table-name Users --attribute-definitions AttributeName=UserId,AttributeType=S --key-schema AttributeName=UserId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 -> Option AQuick Check:
Correct CLI syntax = aws dynamodb create-table --table-name Users --attribute-definitions AttributeName=UserId,AttributeType=S --key-schema AttributeName=UserId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 [OK]
- Using wrong command name like make-table
- Missing attribute-definitions or key-schema
- Incorrect option names like --name instead of --table-name
aws dynamodb create-table --table-name Products --attribute-definitions AttributeName=ProductId,AttributeType=N --key-schema AttributeName=ProductId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
Solution
Step 1: Identify attribute type in attribute-definitions
The attribute ProductId is defined with AttributeType=N, which means Number.Step 2: Confirm key schema uses ProductId as HASH key
The key schema uses ProductId as the partition key, so its type is Number.Final Answer:
Number -> Option DQuick Check:
AttributeType=N means Number [OK]
- Confusing N with String type
- Ignoring attribute-definitions
- Assuming default type is String
aws dynamodb create-table --table-name Orders --attribute-definitions AttributeName=OrderId --key-schema AttributeName=OrderId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
What is the likely cause?
Solution
Step 1: Check attribute-definitions syntax
The attribute-definitions must include AttributeType (S, N, or B). Here, AttributeType is missing.Step 2: Verify other parts
KeyType is present, throughput values are valid, and table name is valid. So the error is due to missing AttributeType.Final Answer:
Missing AttributeType in attribute-definitions -> Option BQuick Check:
AttributeType required in attribute-definitions [OK]
- Omitting AttributeType in attribute-definitions
- Confusing KeyType with AttributeType
- Assuming throughput values cause error
Employees with a composite primary key: EmployeeId (string) as partition key and Department (string) as sort key. Which command correctly creates this table?Solution
Step 1: Define attribute-definitions for both keys
Both EmployeeId and Department must be defined with AttributeType=S.Step 2: Define key-schema with HASH and RANGE keys
EmployeeId is partition key (HASH), Department is sort key (RANGE).Step 3: Check command syntax
aws dynamodb create-table --table-name Employees --attribute-definitions AttributeName=EmployeeId,AttributeType=S AttributeName=Department,AttributeType=S --key-schema AttributeName=EmployeeId,KeyType=HASH AttributeName=Department,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 correctly uses --attribute-definitions and --key-schema with proper syntax and throughput.Final Answer:
aws dynamodb create-table --table-name Employees --attribute-definitions AttributeName=EmployeeId,AttributeType=S AttributeName=Department,AttributeType=S --key-schema AttributeName=EmployeeId,KeyType=HASH AttributeName=Department,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 -> Option AQuick Check:
Composite key needs both keys in attribute-definitions and key-schema [OK]
- Defining only partition key without sort key
- Incorrect key-schema syntax
- Missing attribute definition for sort key
