Why Scan reads the entire table in DynamoDB - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a Scan operation in DynamoDB, it looks through the whole table to find data. Understanding how this affects time helps us know why it can be slow for big tables.
We want to see how the work grows as the table gets bigger.
Analyze the time complexity of the following code snippet.
const params = {
TableName: "MyTable"
};
const data = await dynamodb.scan(params).promise();
console.log(data.Items);
This code scans the entire "MyTable" to get all items without filtering.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each item in the table one by one.
- How many times: Once for every item in the table.
As the table gets bigger, the scan reads more items, so the work grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reads 10 items |
| 100 | Reads 100 items |
| 1000 | Reads 1000 items |
Pattern observation: The work grows evenly as the table size grows.
Time Complexity: O(n)
This means the time to complete the scan grows directly with the number of items in the table.
[X] Wrong: "Scan only reads some items, so it's always fast."
[OK] Correct: Scan reads every item in the table, so it takes longer as the table grows.
Knowing how Scan works helps you explain why some queries are slow and how to choose better ways to get data. This skill shows you understand how databases handle data behind the scenes.
"What if we used a Query operation with a key condition instead of Scan? How would the time complexity change?"
Practice
Scan operation in DynamoDB read the entire table?Solution
Step 1: Understand Scan operation behavior
Scan reads every item in the table one by one to find matching data because it does not use keys or indexes.Step 2: Compare with other operations
Unlike Query, which uses keys to find items quickly, Scan must read the whole table.Final Answer:
Because it checks every item to find matches without using keys -> Option CQuick Check:
Scan reads all items = B [OK]
- Thinking Scan reads only some items
- Confusing Scan with Query
- Assuming Scan uses indexes
Solution
Step 1: Identify Scan method usage
The Scan operation uses thescanmethod with the table name as a parameter.Step 2: Differentiate from other methods
queryis for key-based queries,getItemfetches a single item, andupdatemodifies items.Final Answer:
dynamodb.scan({ TableName: 'MyTable' }, callback); -> Option BQuick Check:
Scan uses scan() method = A [OK]
- Using query() instead of scan()
- Confusing getItem() with scan()
- Using update() for reading data
Solution
Step 1: Understand Scan without filters
Scan reads every item in the table and returns all items if no filter is applied.Step 2: Confirm behavior on item count
Since the table has 1000 items, Scan returns all 1000 items.Final Answer:
It returns all 1000 items by reading the entire table -> Option AQuick Check:
Scan without filter returns all items = A [OK]
- Assuming Scan returns only some items by default
- Confusing Scan with Query filtering
- Thinking Scan returns no items without filter
const params = { TableName: 'MyTable' };
dynamodb.scan(params, (err, data) => {
if (err) console.log(err);
else console.log(data.Items);
});Solution
Step 1: Recognize Scan pagination behavior
Scan returns results in pages. If the table is large, it returns a subset and a LastEvaluatedKey to continue.Step 2: Identify missing pagination handling
The code does not check for LastEvaluatedKey or continue scanning, so it only logs the first page.Final Answer:
Scan returns paginated results; you must handle LastEvaluatedKey to get all items -> Option DQuick Check:
Scan pagination needs LastEvaluatedKey handling = D [OK]
- Assuming Scan returns all items in one call
- Confusing Scan with Query filters
- Using query() instead of scan()
Solution
Step 1: Understand Scan inefficiency
Scan reads every item in the table, which is slow and costly for large tables.Step 2: Use Query with indexes
Creating a Global Secondary Index on 'status' allows Query to quickly find items where 'status'='active' without scanning all items.Final Answer:
Scan reads the entire table which is slow; better to use Query with a Global Secondary Index on 'status' -> Option AQuick Check:
Use Query with index, not Scan for filtering = C [OK]
- Thinking Scan is always fast
- Assuming Scan uses indexes automatically
- Confusing Scan with update or delete operations
