Filter expressions in DynamoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using filter expressions in DynamoDB, it's important to know how the time to get results changes as the data grows.
We want to understand how filtering affects the work DynamoDB does behind the scenes.
Analyze the time complexity of the following DynamoDB scan with a filter expression.
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 filters items where the price is greater than 100.
Look for repeated work done during the scan and filtering.
- Primary operation: Reading each item in the table one by one.
- How many times: Once for every item in the table, because scan reads all items.
As the number of items grows, the scan reads more items, checking each against the filter.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 item reads and filter checks |
| 100 | 100 item reads and filter checks |
| 1000 | 1000 item reads and filter checks |
Pattern observation: The work grows directly with the number of items in the table.
Time Complexity: O(n)
This means the time to complete the scan with filter grows linearly as the table size grows.
[X] Wrong: "Filter expressions make the scan only read matching items."
[OK] Correct: The scan reads every item first, then applies the filter, so all items are processed regardless.
Understanding how filter expressions affect scan time helps you explain real-world trade-offs when working with large datasets.
"What if we replaced scan with a query using a key condition and filter expression? How would the time complexity change?"
Practice
FilterExpression in a DynamoDB scan or query?Solution
Step 1: Understand what FilterExpression does
A FilterExpression is used to specify conditions that items must meet to be returned after the scan or query reads the data.Step 2: Differentiate from other operations
It does not change the table or speed up writes; it only filters results after reading.Final Answer:
To return only items that meet specific conditions after reading all data -> Option AQuick Check:
FilterExpression filters results after reading [OK]
- Thinking FilterExpression reduces read capacity usage
- Confusing FilterExpression with key condition
- Assuming it changes the table data
status equals 'active' in DynamoDB?Solution
Step 1: Recall DynamoDB FilterExpression syntax
DynamoDB uses single equals sign (=) for equality in FilterExpressions.Step 2: Check each option
FilterExpression: "status = 'active'" uses correct syntax. The other options use invalid operators (==, equals, ===) or keywords.Final Answer:
FilterExpression: "status = 'active'" -> Option AQuick Check:
Use single equals (=) for equality in FilterExpression [OK]
- Using double or triple equals (==, ===)
- Writing 'equals' as a word
- Using incorrect operators
age, what will be the result of this scan with FilterExpression age < 30 if the table has ages 25, 30, 35, and 40?Solution
Step 1: Understand the FilterExpression condition
The condition isage < 30, meaning only items with age less than 30 pass the filter.Step 2: Check which ages satisfy the condition
From the list (25, 30, 35, 40), only 25 is less than 30.Final Answer:
Items with ages 25 only -> Option BQuick Check:
age < 30 means less than 30 [OK]
- Including age 30 when condition is less than 30
- Confusing < with <= operator
- Assuming filter changes data in table
contains(name, 'John') but it returns no results even though some items have 'John' in their name. What is the likely mistake?Solution
Step 1: Understand contains() behavior
contains() checks if the attribute contains the exact substring, including case.Step 2: Consider case sensitivity
If the data has 'john' lowercase but the filter uses 'John' with uppercase J, no match occurs.Step 3: Check other choices
Using equals() instead of contains() is not the issue since contains() is appropriate for substrings; reserved words would cause a different error; FilterExpressions are not required to be uppercase.Final Answer:
FilterExpression is case-sensitive and 'John' does not match 'john' -> Option CQuick Check:
contains() is case-sensitive [OK]
- Ignoring case sensitivity in contains()
- Not using ExpressionAttributeNames when needed
- Assuming contains() works like equals()
score is greater than 50 and status is 'active'. Which FilterExpression correctly applies both conditions?Solution
Step 1: Understand logical operators in FilterExpression
DynamoDB uses uppercase AND and OR for combining conditions.Step 2: Check each option's syntax
FilterExpression: "score > 50 AND status = 'active'" uses correct syntax with AND and single equals. FilterExpression: "score > 50 OR status = 'active'" uses OR which is incorrect for both conditions. FilterExpression: "score > 50, status = 'active'" uses comma which is invalid. FilterExpression: "score > 50 && status == 'active'" uses && and double equals which are invalid.Final Answer:
FilterExpression: "score > 50 AND status = 'active'" -> Option DQuick Check:
Use AND in uppercase and single '=' for equality [OK]
- Using OR instead of AND for both conditions
- Using commas or && instead of AND
- Using double equals (==) instead of single equals (=)
