Bird
Raised Fist0
DynamoDBquery~10 mins

Why access patterns drive design in DynamoDB - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the primary key attribute name in a DynamoDB query.

DynamoDB
KeyConditionExpression = '#pk = :[1]'
Drag options to blanks, or click blank then click option'
Apartition_key
BuserId
Cpk_value
DpartitionKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using an attribute name that does not exist in the table schema.
Confusing partition key with sort key names.
2fill in blank
medium

Complete the code to specify the index name for a DynamoDB query to support a specific access pattern.

DynamoDB
IndexName='[1]'
Drag options to blanks, or click blank then click option'
AGSI1
BPrimaryIndex
CLocalIndex
DSecondaryIndex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the primary index name instead of the GSI name.
Using incorrect index names that do not exist.
3fill in blank
hard

Fix the error in the DynamoDB query expression to correctly filter items by sort key.

DynamoDB
KeyConditionExpression = '#pk = :userId AND [1] = :sortKeyVal'
Drag options to blanks, or click blank then click option'
Ask
Bsort_key
CsortKeyVal
DsortKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using the full name 'sortKey' instead of the attribute name.
Using the value variable name instead of the attribute name.
4fill in blank
hard

Fill both blanks to create a DynamoDB query that uses a partition key and a begins_with condition on the sort key.

DynamoDB
KeyConditionExpression = '#pk = :userId AND begins_with(#[1], :[2])'
Drag options to blanks, or click blank then click option'
Ask
BsortKey
Cprefix
DskPrefix
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute names for the sort key.
Confusing the prefix variable name with the attribute name.
5fill in blank
hard

Fill all three blanks to build a DynamoDB query that filters items by partition key, sort key greater than a value, and uses a specific index.

DynamoDB
response = table.query(IndexName='[1]', KeyConditionExpression='#pk = :userId AND #sk [2] :sortVal', ExpressionAttributeNames={'#pk': 'userId', '#sk': '[3]'}, ExpressionAttributeValues={':userId': '123', ':sortVal': '2023-01-01'})
Drag options to blanks, or click blank then click option'
AGSI2
B>
CcreatedAt
DPrimaryIndex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the primary index name instead of the GSI.
Using '=' instead of '>' for the sort key condition.
Mismatching attribute names in ExpressionAttributeNames.

Practice

(1/5)
1. Why is it important to design your DynamoDB table based on access patterns?
easy
A. Because it helps organize data for fast and efficient queries
B. Because it reduces the cost of storage space
C. Because it allows unlimited data size without limits
D. Because it automatically creates indexes for all queries

Solution

  1. Step 1: Understand what access patterns mean

    Access patterns describe how your application will read and write data.
  2. Step 2: Connect access patterns to table design

    Designing tables based on access patterns ensures queries are fast and efficient without scanning.
  3. Final Answer:

    Because it helps organize data for fast and efficient queries -> Option A
  4. Quick Check:

    Access patterns drive design = fast queries [OK]
Hint: Design tables by queries you need, not by data alone [OK]
Common Mistakes:
  • Thinking storage size is the main factor
  • Assuming DynamoDB auto-creates all needed indexes
  • Ignoring how the app will query data
2. Which of the following is the correct way to define a primary key in DynamoDB to support a specific access pattern?
easy
A. PrimaryKey: { SortKey: 'UserId' }
B. PrimaryKey: { PartitionKey: 'OrderId', SortKey: 'UserName' }
C. PrimaryKey: { PartitionKey: 'Date' }
D. PrimaryKey: { PartitionKey: 'UserId', SortKey: 'OrderId' }

Solution

  1. Step 1: Recall DynamoDB primary key structure

    Primary key can be simple (partition key only) or composite (partition + sort key).
  2. Step 2: Check which option uses valid composite key

    PrimaryKey: { PartitionKey: 'UserId', SortKey: 'OrderId' } correctly defines partition key 'UserId' and sort key 'OrderId', matching common access patterns.
  3. Final Answer:

    <code>PrimaryKey: { PartitionKey: 'UserId', SortKey: 'OrderId' }</code> -> Option D
  4. Quick Check:

    Composite key needs PartitionKey + SortKey [OK]
Hint: Primary key needs partition key; add sort key if needed [OK]
Common Mistakes:
  • Using sort key without partition key
  • Confusing attribute names for keys
  • Defining only sort key without partition key
3. Given a DynamoDB table with primary key { PartitionKey: 'CustomerId', SortKey: 'OrderDate' }, which query will efficiently retrieve all orders for customer '123' sorted by date?
medium
A. Query(TableName='Orders', KeyConditionExpression='OrderDate = :date', ExpressionAttributeValues={':date': '2023-01-01'})
B. Scan(TableName='Orders', FilterExpression='CustomerId = :cid', ExpressionAttributeValues={':cid': '123'})
C. Query(TableName='Orders', KeyConditionExpression='CustomerId = :cid', ExpressionAttributeValues={':cid': '123'})
D. Scan(TableName='Orders')

Solution

  1. Step 1: Understand query vs scan in DynamoDB

    Query uses keys to efficiently find items; scan reads entire table and filters after.
  2. Step 2: Match query to access pattern

    Query(TableName='Orders', KeyConditionExpression='CustomerId = :cid', ExpressionAttributeValues={':cid': '123'}) queries by partition key 'CustomerId' = '123', which is efficient and returns sorted by 'OrderDate'.
  3. Final Answer:

    <code>Query(TableName='Orders', KeyConditionExpression='CustomerId = :cid', ExpressionAttributeValues={':cid': '123'})</code> -> Option C
  4. Quick Check:

    Query by partition key = efficient retrieval [OK]
Hint: Use Query with partition key, not Scan, for fast results [OK]
Common Mistakes:
  • Using Scan instead of Query for key-based retrieval
  • Querying by sort key alone without partition key
  • Not using KeyConditionExpression properly
4. You designed a DynamoDB table with primary key { PartitionKey: 'UserId' } but your app needs to query orders by OrderDate. What is the main problem and how to fix it?
medium
A. Problem: Table size is too big; Fix: Use smaller partition keys
B. Problem: Cannot query by OrderDate efficiently; Fix: Add OrderDate as sort key
C. Problem: UserId is missing; Fix: Add UserId as partition key
D. Problem: Query syntax error; Fix: Use Scan instead of Query

Solution

  1. Step 1: Identify the limitation of current key design

    With only partition key UserId, you cannot efficiently query by OrderDate alone.
  2. Step 2: Suggest design fix for access pattern

    Adding OrderDate as sort key allows queries by UserId and ordered by OrderDate efficiently.
  3. Final Answer:

    Problem: Cannot query by OrderDate efficiently; Fix: Add OrderDate as sort key -> Option B
  4. Quick Check:

    Sort key enables range queries by date [OK]
Hint: Add sort key for secondary query attribute [OK]
Common Mistakes:
  • Trying to query by non-key attribute without index
  • Using Scan instead of fixing key design
  • Ignoring need for composite key for multiple access patterns
5. You have two access patterns: (1) Get all orders by CustomerId sorted by OrderDate, and (2) Get all orders by ProductId. How should you design your DynamoDB table to support both efficiently?
hard
A. Use primary key with PartitionKey=CustomerId and SortKey=OrderDate; create a Global Secondary Index with PartitionKey=ProductId
B. Use PartitionKey=ProductId and SortKey=OrderDate; create a Local Secondary Index with PartitionKey=CustomerId
C. Use PartitionKey=OrderDate only; scan table for CustomerId and ProductId queries
D. Use PartitionKey=CustomerId only; filter ProductId in application code

Solution

  1. Step 1: Design primary key for main access pattern

    Primary key with CustomerId as partition key and OrderDate as sort key supports pattern (1) efficiently.
  2. Step 2: Add Global Secondary Index (GSI) for second pattern

    GSI with ProductId as partition key supports pattern (2) without scanning the main table.
  3. Final Answer:

    Use primary key with PartitionKey=CustomerId and SortKey=OrderDate; create a Global Secondary Index with PartitionKey=ProductId -> Option A
  4. Quick Check:

    Primary key + GSI supports multiple access patterns [OK]
Hint: Use GSIs for additional access patterns beyond primary key [OK]
Common Mistakes:
  • Using only one key and scanning for other queries
  • Confusing Local Secondary Index with Global Secondary Index
  • Not adding sort key for ordered queries
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard