📖 Scenario: You are building a DynamoDB table to store information about online orders for a small e-commerce business. You want to efficiently query orders by customer and by order status using Global Secondary Indexes (GSIs).
🎯 Goal: Create a DynamoDB table with a primary key and add a Global Secondary Index (GSI) with a suitable partition key and sort key to support querying orders by customer and order status.
📋 What You'll Learn
Create a DynamoDB table named Orders with OrderId as the partition key.
Add a GSI named CustomerStatusIndex with CustomerId as the partition key and OrderStatus as the sort key.
Use the correct attribute definitions for all keys.
Set the projection type of the GSI to ALL.
💡 Why This Matters
🌍 Real World
E-commerce platforms often need to query orders by customer and status quickly. Using GSIs with well-chosen keys improves query speed and reduces costs.
💼 Career
Understanding how to design GSIs is essential for database engineers and backend developers working with DynamoDB to optimize data access patterns.
Progress0 / 4 steps
1
Create the DynamoDB table with primary key
Create a DynamoDB table named Orders with OrderId as the partition key of type S (string). Define the attribute OrderId in the attribute definitions.
DynamoDB
Hint
Use OrderId as the HASH key in KeySchema and define it in AttributeDefinitions with type S.
2
Add attribute definitions for GSI keys
Add attribute definitions for CustomerId and OrderStatus with type S (string) to the existing attribute definitions.
DynamoDB
Hint
Remember to add both CustomerId and OrderStatus as string attributes in AttributeDefinitions.
3
Define the Global Secondary Index (GSI)
Add a Global Secondary Index named CustomerStatusIndex with CustomerId as the partition key (HASH) and OrderStatus as the sort key (RANGE). Set the projection type to ALL.
DynamoDB
Hint
Define the GSI with CustomerId as HASH key and OrderStatus as RANGE key, and set projection type to ALL.
4
Complete the table creation configuration
Add the BillingMode set to PAY_PER_REQUEST to the table configuration to enable on-demand capacity mode.
DynamoDB
Hint
Set BillingMode to PAY_PER_REQUEST to use on-demand capacity.
Practice
(1/5)
1. What is the main purpose of choosing a good partition key for a Global Secondary Index (GSI) in DynamoDB?
easy
A. To evenly distribute data across partitions for faster queries
B. To store large binary objects efficiently
C. To encrypt data at rest automatically
D. To backup data to another region
Solution
Step 1: Understand partition key role in GSI
The partition key determines how data is spread across storage partitions in DynamoDB.
Step 2: Importance of even data distribution
Choosing a partition key that evenly distributes data avoids hot partitions and improves query speed.
Final Answer:
To evenly distribute data across partitions for faster queries -> Option A
Quick Check:
Partition key = data distribution [OK]
Hint: Partition key controls data spread, pick for balance [OK]
Common Mistakes:
Thinking partition key orders data inside partitions
Confusing partition key with sort key
Assuming partition key encrypts data
2. Which of the following is the correct syntax to define a GSI with a partition key named Category and a sort key named Timestamp in a DynamoDB table creation JSON?
Partition key uses KeyType "HASH" and sort key uses KeyType "RANGE" in DynamoDB.
Step 2: Match keys to correct KeyType
Category is partition key so it must have KeyType "HASH"; Timestamp is sort key so it must have KeyType "RANGE".
Final Answer:
Correct JSON with Category as HASH and Timestamp as RANGE -> Option B
Quick Check:
Partition key = HASH, Sort key = RANGE [OK]
Hint: Partition key = HASH, sort key = RANGE in KeySchema [OK]
Common Mistakes:
Swapping HASH and RANGE key types
Using RANGE for partition key
Incorrect JSON structure for KeySchema
3. Given a DynamoDB table with a GSI where the partition key is UserId and the sort key is OrderDate, what will be the result of this query?
Query on GSI with UserId = 'user123' and OrderDate between '2023-01-01' and '2023-01-31'
medium
A. All orders by user123 in January 2023 sorted by OrderDate
B. All orders by all users in January 2023
C. All orders by user123 regardless of date
D. Syntax error due to missing filter expression
Solution
Step 1: Understand query conditions on GSI keys
Query uses partition key UserId = 'user123' and a range condition on sort key OrderDate between two dates.
Step 2: Result filters by UserId and date range
Only items with UserId 'user123' and OrderDate in January 2023 are returned, sorted by OrderDate.
Final Answer:
All orders by user123 in January 2023 sorted by OrderDate -> Option A
Quick Check:
Partition key + sort key range = filtered query [OK]
Hint: Query filters partition key exactly, sort key by range [OK]
Common Mistakes:
Thinking query returns all users' data
Ignoring sort key range condition
Assuming syntax error without filter expression
4. You created a GSI with partition key Category and sort key Price. Your queries on this GSI are slow and uneven. What is the most likely cause?
medium
A. Price is not indexed as a sort key
B. The table has no primary key
C. You forgot to project attributes
D. Category has very few distinct values causing hot partitions
Solution
Step 1: Analyze partition key distribution
If Category has few distinct values, many items share the same partition key, causing hot partitions and slow queries.
Step 2: Understand impact on query performance
Hot partitions limit throughput and cause uneven query speed despite having a sort key.
Final Answer:
Category has very few distinct values causing hot partitions -> Option D
Quick Check:
Few partition keys = hot partitions = slow queries [OK]
Hint: Few partition key values cause hot partitions [OK]
Common Mistakes:
Assuming missing sort key causes slowness
Ignoring partition key cardinality
Thinking projection affects partitioning
5. You want to design a GSI to efficiently query orders by CustomerId and sort them by OrderStatus and then by OrderDate. Which key selection strategy is best?
hard
A. Partition key: CustomerId; Sort key: OrderDate only
B. Partition key: OrderStatus; Sort key: CustomerId#OrderDate concatenated string
C. Partition key: CustomerId; Sort key: OrderStatus#OrderDate concatenated string
D. Partition key: OrderDate; Sort key: CustomerId
Solution
Step 1: Identify query pattern requirements
You want to query by CustomerId and sort first by OrderStatus, then by OrderDate.
Step 2: Design composite sort key for multi-level sorting
Concatenate OrderStatus and OrderDate into one sort key string (e.g., 'Shipped#2023-06-01') to sort by status then date.
Step 3: Assign keys correctly
Partition key should be CustomerId for grouping orders by customer; sort key is the concatenated string for sorting.