Scan with filter expressions in DynamoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using DynamoDB's scan with filter expressions, we want to know how the time to get results changes as the table grows.
We ask: How does the scan operation's cost grow when the table has more items?
Analyze the time complexity of the following code snippet.
const params = {
TableName: "Products",
FilterExpression: "Price > :minPrice",
ExpressionAttributeValues: {
":minPrice": { N: "100" }
}
};
const result = await dynamodb.scan(params).promise();
This code scans the entire "Products" table and returns only items where the Price is greater than 100.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each item in the table to check the filter condition.
- How many times: Once for every item in the table, regardless of the filter.
As the number of items in the table grows, the scan checks each item once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 item checks |
| 100 | 100 item checks |
| 1000 | 1000 item checks |
Pattern observation: The number of operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to scan grows linearly as the table gets bigger.
[X] Wrong: "The filter expression makes the scan only look at matching items, so it's fast even for big tables."
[OK] Correct: The scan still reads every item; the filter only removes items after reading them, so the work grows with table size.
Understanding how scan with filters works helps you explain performance trade-offs clearly, a useful skill when designing or troubleshooting databases.
"What if we replaced scan with a query using a key condition? How would the time complexity change?"
Practice
Scan operation with a filter expression do in DynamoDB?Solution
Step 1: Understand Scan operation
A Scan reads every item in the table regardless of any condition.Step 2: Apply filter expression effect
The filter expression is applied after reading all items, so only matching items are returned.Final Answer:
It reads all items but returns only those matching the filter condition. -> Option DQuick Check:
Scan + filter = read all, return filtered [OK]
- Thinking Scan reads only filtered items
- Confusing Scan with Query
- Assuming filter modifies data
Solution
Step 1: Identify correct parameter name
The correct parameter for filtering in Scan isFilterExpression.Step 2: Check syntax correctness
scan(TableName='MyTable', FilterExpression='attribute_exists(Name)') usesFilterExpressionwith a valid conditionattribute_exists(Name).Final Answer:
scan(TableName='MyTable', FilterExpression='attribute_exists(Name)') -> Option CQuick Check:
FilterExpression is correct parameter [OK]
- Using ConditionExpression instead of FilterExpression
- Using Filter or FilterCondition which are invalid
- Incorrect syntax for filter condition
Age > 30?Solution
Step 1: Understand filter condition
The filter expressionAge > 30means only items with Age greater than 30 are returned.Step 2: Check each item against condition
Alice has Age 30 (not greater), Bob 25 (not greater), Carol 35 (greater). Only Carol matches.Final Answer:
[{"Name": "Carol", "Age": 35}] -> Option BQuick Check:
Age > 30 returns Carol only [OK]
- Including items with Age equal to 30
- Confusing greater than with greater or equal
- Returning all items ignoring filter
FilterExpression='Age > :val' and ExpressionAttributeValues={':val': 30}, but it returns no items. What is the likely error?Solution
Step 1: Check ExpressionAttributeValues format
DynamoDB expects attribute values in a typed format, e.g., numbers as {'N': '30'}.Step 2: Identify why no items returned
Providing raw number 30 instead of typed value causes filter to fail matching any item.Final Answer:
ExpressionAttributeValues must be a dictionary with DynamoDB types, e.g., {':val': {'N': '30'}}. -> Option AQuick Check:
Use typed values in ExpressionAttributeValues [OK]
- Passing raw Python values instead of typed dict
- Using HTML entities like > in code instead of >
- Thinking Scan does not support filters
Status is 'Active' and Score is greater than 80. Which filter expression and attribute values are correct?Solution
Step 1: Check filter expression logic
The condition requires both Status equals 'Active' AND Score greater than 80, so use 'AND' and '=' operators correctly.Step 2: Verify ExpressionAttributeValues format
Values must be typed: strings as {'S': 'Active'} and numbers as {'N': '80'}.Final Answer:
FilterExpression='Status = :s AND Score > :sc', ExpressionAttributeValues={':s': {'S': 'Active'}, ':sc': {'N': '80'}} -> Option AQuick Check:
Correct logic and typed values [OK]
- Using '==' instead of '=' in filter expression
- Using OR instead of AND
- Passing untyped values in ExpressionAttributeValues
