Bird
Raised Fist0
DynamoDBquery~10 mins

Scan vs Query performance comparison in DynamoDB - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform a Query operation on a DynamoDB table.

DynamoDB
response = table.[1](KeyConditionExpression=Key('UserId').eq('123'))
Drag options to blanks, or click blank then click option'
Aget_item
Bscan
Cquery
Dput_item
Attempts:
3 left
💡 Hint
Common Mistakes
Using scan instead of query for key-based retrieval.
2fill in blank
medium

Complete the code to perform a Scan operation on a DynamoDB table.

DynamoDB
response = table.[1]()
Drag options to blanks, or click blank then click option'
Aquery
Bscan
Cget_item
Ddelete_item
Attempts:
3 left
💡 Hint
Common Mistakes
Using query when wanting to read all items.
3fill in blank
hard

Fix the error in the code to improve performance by using the correct method.

DynamoDB
response = table.[1](KeyConditionExpression=Key('Status').eq('Active'))
Drag options to blanks, or click blank then click option'
Aquery
Bput_item
Cget_item
Dscan
Attempts:
3 left
💡 Hint
Common Mistakes
Using scan with filter instead of query with key condition.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that shows item counts for active users using Query.

DynamoDB
active_users = {item['UserId']: item['Count'] for item in table.[1](KeyConditionExpression=Key('Status').eq('Active')).[2]
Drag options to blanks, or click blank then click option'
Aquery
Bscan
CItems
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using scan instead of query, or accessing wrong attribute for items.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters items with Query and a condition.

DynamoDB
filtered = {item['Id']: item['Value'] for item in table.[1](KeyConditionExpression=Key('Category').eq('Books')).[2] if item['Value'] [3] 100}
Drag options to blanks, or click blank then click option'
Aquery
BItems
C>
Dscan
Attempts:
3 left
💡 Hint
Common Mistakes
Using scan instead of query, or wrong comparison operator.

Practice

(1/5)
1. Which DynamoDB operation is generally faster when you know the partition key of the item you want to retrieve?
easy
A. Scan
B. UpdateItem
C. Query
D. DeleteItem

Solution

  1. Step 1: Understand Query operation

    Query uses the partition key to directly find matching items, making it efficient.
  2. Step 2: Compare with Scan operation

    Scan reads the entire table, which is slower and less efficient.
  3. Final Answer:

    Query -> Option C
  4. Quick Check:

    Query is faster for known keys [OK]
Hint: Use Query when you know the partition key for speed [OK]
Common Mistakes:
  • Thinking Scan is faster because it reads all data
  • Confusing Query with Scan
  • Assuming UpdateItem is for reading data
2. Which of the following is the correct syntax to perform a Query operation in DynamoDB using AWS SDK for JavaScript?
easy
A. dynamoDbClient.query({ TableName: 'MyTable', KeyConditionExpression: '#pk = :pkval', ExpressionAttributeNames: { '#pk': 'PartitionKey' }, ExpressionAttributeValues: { ':pkval': '123' } })
B. dynamoDbClient.scan({ TableName: 'MyTable', KeyConditionExpression: 'PartitionKey = 123' })
C. dynamoDbClient.query({ TableName: 'MyTable', FilterExpression: 'PartitionKey = 123' })
D. dynamoDbClient.getItem({ TableName: 'MyTable', Key: { PartitionKey: '123' } })

Solution

  1. Step 1: Identify correct Query syntax

    Query requires KeyConditionExpression with placeholders and attribute names/values.
  2. 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.
  3. Final Answer:

    dynamoDbClient.query({ TableName: 'MyTable', KeyConditionExpression: '#pk = :pkval', ExpressionAttributeNames: { '#pk': 'PartitionKey' }, ExpressionAttributeValues: { ':pkval': '123' } }) -> Option A
  4. Quick Check:

    Query needs KeyConditionExpression [OK]
Hint: Query needs KeyConditionExpression, not FilterExpression [OK]
Common Mistakes:
  • Using FilterExpression instead of KeyConditionExpression for Query
  • Using scan method with KeyConditionExpression
  • Confusing getItem with query syntax
3. Given a DynamoDB table with 1000 items, what will be the main difference in performance between these two operations?
dynamoDbClient.scan({ TableName: 'MyTable' })
and
dynamoDbClient.query({ TableName: 'MyTable', KeyConditionExpression: '#pk = :pk', ExpressionAttributeNames: { '#pk': 'PartitionKey' }, ExpressionAttributeValues: { ':pk': '123' } })
medium
A. Scan reads all 1000 items; Query reads only matching items, so Query is faster.
B. Scan is faster because it reads all items at once; Query is slower due to filtering.
C. Both operations have the same speed because they access the same table.
D. Query reads all items; Scan reads only matching items.

Solution

  1. Step 1: Understand Scan operation

    Scan reads every item in the table, so it processes all 1000 items.
  2. Step 2: Understand Query operation

    Query uses the partition key to read only matching items, which is faster.
  3. Final Answer:

    Scan reads all items; Query reads only matching items, so Query is faster. -> Option A
  4. Quick Check:

    Scan reads all; Query reads matching [OK]
Hint: Scan reads whole table; Query reads only matching keys [OK]
Common Mistakes:
  • Thinking Scan is faster because it reads all data at once
  • Confusing Query reading all items
  • Assuming both have same speed
4. You wrote this DynamoDB Query code but it returns no results:
const params = { TableName: 'MyTable', KeyConditionExpression: 'PartitionKey = :pk', ExpressionAttributeValues: { ':pk': '123' } };
const data = await dynamoDbClient.query(params);

What is the most likely error?
medium
A. Missing ExpressionAttributeNames for reserved word PartitionKey
B. Using Scan instead of Query
C. TableName is misspelled
D. Incorrect KeyConditionExpression syntax; should use #pk = :pk

Solution

  1. Step 1: Check KeyConditionExpression syntax

    KeyConditionExpression requires placeholders for attribute names like #pk defined in ExpressionAttributeNames.
  2. Step 2: Identify missing ExpressionAttributeNames

    The code uses 'PartitionKey' directly without ExpressionAttributeNames, causing no matches.
  3. Final Answer:

    Incorrect KeyConditionExpression syntax; should use #pk = :pk -> Option D
  4. Quick Check:

    Reserved words need placeholders in KeyConditionExpression [OK]
Hint: Use placeholders for reserved words in KeyConditionExpression [OK]
Common Mistakes:
  • Not using ExpressionAttributeNames for reserved words
  • Confusing Scan and Query methods
  • Misspelling TableName
5. You want to retrieve all items where the attribute 'Status' equals 'Active' from a large DynamoDB table. The table's partition key is 'UserId'. Which approach is best for performance and cost?
hard
A. Use Query with KeyConditionExpression on 'UserId' and FilterExpression on 'Status' = 'Active'.
B. Create a Global Secondary Index (GSI) on 'Status' and Query the GSI for 'Active' items.
C. Use Scan with a FilterExpression on 'Status' = 'Active' to get all matching items.
D. Use Scan without any filters to get all items and then filter in application code.

Solution

  1. 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.
  2. Step 2: Use GSI for efficient querying

    Creating a GSI on 'Status' allows Query on 'Status' attribute efficiently without scanning.
  3. Final Answer:

    Create a Global Secondary Index (GSI) on 'Status' and Query the GSI for 'Active' items. -> Option B
  4. Quick Check:

    GSI enables efficient queries on non-key attributes [OK]
Hint: Use GSI to query non-key attributes efficiently [OK]
Common Mistakes:
  • Using Scan with filters on large tables
  • Trying to Query without partition key
  • Filtering in application instead of database