Bird
0
0

You wrote this query to get all comments for a post in a single-table design DynamoDB table:

medium📝 Debug Q14 of 15
DynamoDB - Access Patterns and Query Optimization
You wrote this query to get all comments for a post in a single-table design DynamoDB table:
KeyConditionExpression: PK = 'POST#123' AND SK = 'COMMENT#'

Why does this query return no results?
ABecause SK uses '=' instead of 'begins_with' to match multiple comments.
BBecause PK should be 'COMMENT#' and SK should be 'POST#123'.
CBecause the table does not support queries on SK.
DBecause the query syntax is missing a FilterExpression.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the KeyConditionExpression

    The query uses SK = 'COMMENT#', which looks for an exact match on SK.
  2. Step 2: Understand why no results appear

    Comments likely have SK starting with 'COMMENT#' plus an ID, so exact match fails. Using 'begins_with' on SK is needed to get all comments.
  3. Final Answer:

    Because SK uses '=' instead of 'begins_with' to match multiple comments. -> Option A
  4. Quick Check:

    Use begins_with for multiple SK matches [OK]
Quick Trick: Use begins_with for partial sort key matches [OK]
Common Mistakes:
MISTAKES
  • Using '=' instead of begins_with on SK
  • Swapping PK and SK in query
  • Assuming FilterExpression fixes key condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes