Scan vs Query performance comparison in DynamoDB - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When working with DynamoDB, it is important to know how fast your data requests run.
We want to see how the time to get data changes when using Scan versus Query.
Analyze the time complexity of these DynamoDB operations.
// Query example
const paramsQuery = {
TableName: "Users",
KeyConditionExpression: "UserId = :id",
ExpressionAttributeValues: { ":id": { S: "123" } }
};
// Scan example
const paramsScan = {
TableName: "Users"
};
The Query fetches items by a specific key, while Scan reads the whole table.
Look at what repeats as the table grows.
- Primary operation: Scan reads every item in the table; Query reads only matching items.
- How many times: Scan checks all items (n times); Query checks only items with the key (usually fewer).
Think about how many items the database looks at as the table gets bigger.
| Input Size (n) | Approx. Operations (Scan) | Approx. Operations (Query) |
|---|---|---|
| 10 | 10 | 1-2 |
| 100 | 100 | 1-5 |
| 1000 | 1000 | 1-10 |
Pattern observation: Scan grows directly with table size; Query stays small if keys are selective.
Time Complexity: O(n) for Scan, O(1) for Query
Scan time grows with table size; Query time stays mostly constant by using keys.
[X] Wrong: "Query always scans the whole table like Scan."
[OK] Correct: Query uses keys to jump directly to matching items, so it does not read everything.
Understanding how Scan and Query scale helps you design fast database requests and shows you know how to handle big data.
"What if we add a filter to Scan? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand Query operation
Query uses the partition key to directly find matching items, making it efficient.Step 2: Compare with Scan operation
Scan reads the entire table, which is slower and less efficient.Final Answer:
Query -> Option CQuick Check:
Query is faster for known keys [OK]
- Thinking Scan is faster because it reads all data
- Confusing Query with Scan
- Assuming UpdateItem is for reading data
Solution
Step 1: Identify correct Query syntax
Query requires KeyConditionExpression with placeholders and attribute names/values.Step 2: Check options for correct usage
dynamoDbClient.query({ TableName: 'MyTable', KeyConditionExpression: '#pk = :pkval', ExpressionAttributeNames: { '#pk': 'PartitionKey' }, ExpressionAttributeValues: { ':pkval': '123' } }) uses KeyConditionExpression and ExpressionAttributeNames/Values correctly.Final Answer:
dynamoDbClient.query({ TableName: 'MyTable', KeyConditionExpression: '#pk = :pkval', ExpressionAttributeNames: { '#pk': 'PartitionKey' }, ExpressionAttributeValues: { ':pkval': '123' } }) -> Option AQuick Check:
Query needs KeyConditionExpression [OK]
- Using FilterExpression instead of KeyConditionExpression for Query
- Using scan method with KeyConditionExpression
- Confusing getItem with query syntax
dynamoDbClient.scan({ TableName: 'MyTable' })and
dynamoDbClient.query({ TableName: 'MyTable', KeyConditionExpression: '#pk = :pk', ExpressionAttributeNames: { '#pk': 'PartitionKey' }, ExpressionAttributeValues: { ':pk': '123' } })Solution
Step 1: Understand Scan operation
Scan reads every item in the table, so it processes all 1000 items.Step 2: Understand Query operation
Query uses the partition key to read only matching items, which is faster.Final Answer:
Scan reads all items; Query reads only matching items, so Query is faster. -> Option AQuick Check:
Scan reads all; Query reads matching [OK]
- Thinking Scan is faster because it reads all data at once
- Confusing Query reading all items
- Assuming both have same speed
const params = { TableName: 'MyTable', KeyConditionExpression: 'PartitionKey = :pk', ExpressionAttributeValues: { ':pk': '123' } };
const data = await dynamoDbClient.query(params);What is the most likely error?
Solution
Step 1: Check KeyConditionExpression syntax
KeyConditionExpression requires placeholders for attribute names like #pk defined in ExpressionAttributeNames.Step 2: Identify missing ExpressionAttributeNames
The code uses 'PartitionKey' directly without ExpressionAttributeNames, causing no matches.Final Answer:
Incorrect KeyConditionExpression syntax; should use #pk = :pk -> Option DQuick Check:
Reserved words need placeholders in KeyConditionExpression [OK]
- Not using ExpressionAttributeNames for reserved words
- Confusing Scan and Query methods
- Misspelling TableName
Solution
Step 1: Understand limitations of Scan and Query
Scan reads entire table and is costly; Query requires partition key, but 'Status' is not the partition key.Step 2: Use GSI for efficient querying
Creating a GSI on 'Status' allows Query on 'Status' attribute efficiently without scanning.Final Answer:
Create a Global Secondary Index (GSI) on 'Status' and Query the GSI for 'Active' items. -> Option BQuick Check:
GSI enables efficient queries on non-key attributes [OK]
- Using Scan with filters on large tables
- Trying to Query without partition key
- Filtering in application instead of database
