0
0
DynamoDBquery~5 mins

Scan vs Query performance comparison in DynamoDB

Choose your learning style9 modes available
Introduction

We want to find data quickly and efficiently in DynamoDB. Understanding the difference between Scan and Query helps us choose the faster way.

When you know the exact key of the item you want to find.
When you want to get all items but only a few match your condition.
When you want to read the whole table without filtering.
When you want to save time and reduce cost by reading less data.
When you want to avoid reading unnecessary data from the database.
Syntax
DynamoDB
Query:
  Use KeyConditionExpression to specify the key.
  Optionally use FilterExpression to narrow results.

Scan:
  Reads every item in the table.
  Optionally use FilterExpression to narrow results after reading.

Query is faster because it looks only at items with matching keys.

Scan reads the whole table, so it is slower and costs more.

Examples
This finds all songs by the artist 'No One You Know' quickly using the key.
DynamoDB
Query example:

aws dynamodb query \
  --table-name Music \
  --key-condition-expression "Artist = :artist" \
  --expression-attribute-values '{":artist":{"S":"No One You Know"}}'
This reads the whole Music table and then filters to show only Rock songs.
DynamoDB
Scan example:

aws dynamodb scan \
  --table-name Music \
  --filter-expression "Genre = :genre" \
  --expression-attribute-values '{":genre":{"S":"Rock"}}'
Sample Program

The Query example uses the Artist key to find matching songs fast.

The Scan example reads all songs and then filters, which takes more time.

DynamoDB
SELECT * FROM Music WHERE Artist = 'No One You Know'; -- Query example

-- This returns only songs by 'No One You Know' quickly

-- Scan example (conceptual):
-- Scan the whole Music table and filter Genre = 'Rock'
-- This is slower and reads all items
OutputSuccess
Important Notes

Query uses the primary key or index to find data fast.

Scan reads every item, so it is slower and costs more.

Always prefer Query when you can specify the key.

Summary

Query is fast and efficient for known keys.

Scan reads the whole table and is slower.

Use Query to save time and money whenever possible.