Bird
0
0

How should you define the primary key in DynamoDB to efficiently query items by UserId and then sort them by Timestamp?

easy📝 Syntax Q3 of 15
DynamoDB - Access Patterns and Query Optimization
How should you define the primary key in DynamoDB to efficiently query items by UserId and then sort them by Timestamp?
APartitionKey: UserId only
BPartitionKey: Timestamp, SortKey: UserId
CPartitionKey: UserId, SortKey: Timestamp
DPartitionKey: Timestamp only
Step-by-Step Solution
Solution:
  1. Step 1: Identify Query Pattern

    You want to query by UserId and sort by Timestamp.
  2. Step 2: Define Keys

    PartitionKey should be UserId to group user data; SortKey should be Timestamp to order items.
  3. Final Answer:

    PartitionKey: UserId, SortKey: Timestamp -> Option C
  4. Quick Check:

    PartitionKey groups, SortKey sorts [OK]
Quick Trick: PartitionKey groups, SortKey sorts data [OK]
Common Mistakes:
MISTAKES
  • Reversing PartitionKey and SortKey roles
  • Using only PartitionKey without SortKey for sorted queries
  • Choosing unrelated attributes as keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes