0
0
DynamodbComparisonBeginner · 4 min read

DynamoDB Query vs Scan: Key Differences and When to Use Each

Use Query in DynamoDB when you know the partition key and want to retrieve specific items efficiently. Use Scan when you need to read the entire table or filter items without specifying a key, but it is slower and more costly.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of Query and Scan operations in DynamoDB.

FactorQueryScan
Access PatternRequires partition keyNo key required
PerformanceFast and efficientSlow and costly
Data ReturnedItems matching key and optional filtersAll items in table, filtered after
Use CaseGet specific items or rangesFull table read or broad filtering
CostLower read capacity unitsHigher read capacity units
Result OrderSorted by sort keyNo guaranteed order
⚖️

Key Differences

The Query operation in DynamoDB is designed to find items based on the partition key and optionally a sort key. It is very efficient because it directly looks up the data using the key index. You can also apply filters to narrow down results, but the main selection is done by key.

On the other hand, Scan reads every item in the table and then applies any filters. This means it can be very slow and expensive for large tables because it processes all data. Scan is useful only when you don't know the key or need to examine all items.

In summary, Query is targeted and fast, while Scan is broad and costly. Choosing the right one depends on your data access pattern and performance needs.

⚖️

Code Comparison

python
import boto3
from boto3.dynamodb.conditions import Key

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Products')

# Query to get items with partition key 'Category' = 'Books'
response = table.query(
    KeyConditionExpression=Key('Category').eq('Books')
)
items = response['Items']
print(items)
Output
[{'Category': 'Books', 'ProductID': '123', 'Title': 'Learn DynamoDB'}, {'Category': 'Books', 'ProductID': '456', 'Title': 'Advanced AWS'}]
↔️

Scan Equivalent

python
import boto3
from boto3.dynamodb.conditions import Attr

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Products')

# Scan to get all items where 'Category' = 'Books'
response = table.scan(
    FilterExpression=Attr('Category').eq('Books')
)
items = response['Items']
print(items)
Output
[{'Category': 'Books', 'ProductID': '123', 'Title': 'Learn DynamoDB'}, {'Category': 'Books', 'ProductID': '456', 'Title': 'Advanced AWS'}]
🎯

When to Use Which

Choose Query when:

  • You know the partition key and want fast, efficient lookups.
  • You want to retrieve items sorted by sort key.
  • You want to minimize read costs and latency.

Choose Scan when:

  • You need to examine all items in the table.
  • You don't have the partition key to filter by.
  • You want to apply filters on attributes without keys, accepting slower performance.

In general, prefer Query for targeted access and use Scan sparingly due to its cost and speed.

Key Takeaways

Use Query for fast, efficient lookups when you know the partition key.
Scan reads the entire table and is slower and more expensive.
Query returns sorted results by sort key; Scan does not guarantee order.
Prefer Query to reduce cost and latency in DynamoDB.
Use Scan only when you must read all items or lack key information.