Attribute types (S, N, B, BOOL, L, M) in DynamoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with DynamoDB attribute types, it's important to understand how the time to process data grows as the data size increases.
We want to know how the operations scale when handling different attribute types like strings, numbers, lists, and maps.
Analyze the time complexity of reading and processing various attribute types in DynamoDB.
// Example item with different attribute types
{
"Name": {"S": "Alice"},
"Age": {"N": "30"},
"ProfilePic": {"B": "...binary data..."},
"IsActive": {"BOOL": true},
"Tags": {"L": [{"S": "friend"}, {"S": "colleague"}]},
"Address": {"M": {"City": {"S": "NY"}, "Zip": {"N": "10001"}}}
}
This snippet shows an item with different attribute types being read or processed.
Look for parts that repeat work as data grows.
- Primary operation: Traversing lists (
L) and maps (M) to process each element or key-value pair. - How many times: Once per element in a list or once per key in a map.
As the number of elements in lists or keys in maps grows, the time to process them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations to process list or map elements |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: Processing time grows linearly with the number of elements in lists or maps.
Time Complexity: O(n)
This means the time to process attributes grows directly with the number of items in lists or keys in maps.
[X] Wrong: "Processing a map or list attribute always takes constant time regardless of size."
[OK] Correct: Because each element or key-value pair must be handled individually, so more elements mean more work.
Understanding how attribute types affect processing time helps you explain data handling clearly and shows you grasp how data size impacts performance.
"What if we nested multiple maps inside lists? How would the time complexity change when processing such nested attributes?"
Practice
Which DynamoDB attribute type is used to store plain text strings?
Solution
Step 1: Understand attribute types in DynamoDB
DynamoDB uses attribute types to define data kinds. 'S' stands for string (text).Step 2: Match the type to text data
Since 'S' means string, it is the correct type for plain text.Final Answer:
S -> Option DQuick Check:
Text data = S [OK]
- Confusing N (number) with S (string)
- Choosing BOOL for text
- Using B for text data
Which of the following is the correct syntax to define a boolean attribute named isActive in a DynamoDB item?
{"isActive": {"BOOL": true}}Solution
Step 1: Identify the correct attribute type for boolean
Boolean values in DynamoDB use the BOOL type with true or false.Step 2: Check the syntax for boolean attribute
The correct syntax is {"isActive": {"BOOL": true}} to represent a boolean true.Final Answer:
{"isActive": {"BOOL": true}} -> Option BQuick Check:
Boolean attribute uses BOOL type [OK]
- Using S type with string 'true'
- Using N type with 1 instead of BOOL
- Using B type for boolean
Given the following DynamoDB item attribute, what is the type of tags?
{"tags": {"L": [ {"S": "red"}, {"S": "blue"}, {"S": "green"} ] }}Solution
Step 1: Identify the attribute type key
The attribute uses the key "L", which stands for List in DynamoDB.Step 2: Understand the value structure
The value is an array of strings, confirming it is a list of string elements.Final Answer:
List (L) -> Option AQuick Check:
Attribute key L means List [OK]
- Confusing L with M (map)
- Thinking it's a string because of S inside list
- Choosing B for binary data
Identify the error in this DynamoDB attribute definition:
{"profile": {"M": {"age": {"S": 30}, "name": {"S": "Alice"}}}}Solution
Step 1: Analyze the attribute types inside the map
The map contains "age" with type S and value 30, and "name" with type S and value "Alice".Step 2: Check if types match values
Age is a number (30), so it should use type N, not S (string). Name is correctly a string.Final Answer:
Age attribute should use type N, not S -> Option CQuick Check:
Numbers use N type, not S [OK]
- Using S for numbers
- Thinking maps can't nest attributes
- Confusing name type as number
You want to store a user's profile with a list of phone numbers and a map of address details in DynamoDB. Which attribute types should you use for phoneNumbers and address respectively?
Solution
Step 1: Identify the type for a list of phone numbers
A list of phone numbers is a collection, so it should use the List (L) type.Step 2: Identify the type for address details as key-value pairs
Address details are structured data with keys and values, so Map (M) type is appropriate.Final Answer:
phoneNumbers: L, address: M -> Option AQuick Check:
Lists = L, Maps = M [OK]
- Swapping L and M types
- Using S for lists
- Using BOOL or N incorrectly
