What is a Global Secondary Index (GSI) in DynamoDB?
A GSI is an index with a partition key and optional sort key that can be different from the base table's keys. It allows querying data efficiently using alternate keys.
Click to reveal answer
beginner
Can a GSI have a different partition key than the main table?
Yes, a GSI can have a completely different partition key and sort key from the main table, enabling flexible queries.
Click to reveal answer
intermediate
How do you create a GSI when creating a DynamoDB table?
You specify the GSI in the table creation request by defining its IndexName, KeySchema, Projection, and ProvisionedThroughput (if applicable).
Click to reveal answer
intermediate
What is the purpose of the Projection attribute in a GSI?
Projection defines which attributes from the base table are copied into the GSI. It can be ALL, KEYS_ONLY, or INCLUDE specific attributes.
Click to reveal answer
intermediate
Can you add a GSI to an existing DynamoDB table?
Yes, you can add a GSI to an existing table using the UpdateTable API, but it takes time to build and consumes throughput.
Click to reveal answer
What does a Global Secondary Index allow you to do in DynamoDB?
AEncrypt data at rest
BStore data in a different AWS region
CQuery data using alternate partition and sort keys
DAutomatically backup your table
✗ Incorrect
A GSI lets you query data efficiently using different partition and sort keys than the main table.
Which attribute defines what data is copied into a GSI?
AKeySchema
BProjection
CProvisionedThroughput
DAttributeDefinitions
✗ Incorrect
Projection specifies which attributes from the base table are included in the GSI.
Can a GSI have a sort key different from the main table?
ANo
BOnly for local secondary indexes
COnly if the table has no sort key
DYes
✗ Incorrect
A GSI can have a different partition key and sort key from the main table.
When adding a GSI to an existing table, what should you expect?
ATime to build and throughput consumption
BTable downtime
CInstant availability
DAutomatic data deletion
✗ Incorrect
Adding a GSI requires time to build and uses table throughput during creation.
Which of these is NOT a valid Projection type for a GSI?
AEXCLUDE
BKEYS_ONLY
CINCLUDE
DALL
✗ Incorrect
EXCLUDE is not a valid Projection type; valid types are ALL, KEYS_ONLY, and INCLUDE.
Explain what a Global Secondary Index is and why you would use one in DynamoDB.
Think about how you might want to find data using different keys than the main table.
You got /3 concepts.
Describe the steps and key parameters needed to create a GSI when creating a DynamoDB table.
Focus on the parts of the table creation request related to the GSI.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a Global Secondary Index (GSI) in DynamoDB?
easy
A. To automatically backup the table data
B. To increase the storage capacity of the table
C. To allow querying the table using alternative key attributes
D. To encrypt the table data at rest
Solution
Step 1: Understand what a GSI does
A GSI lets you query a DynamoDB table using different key attributes than the primary key.
Step 2: Compare options to GSI purpose
Options A, B, and D describe backup, storage, and encryption, which are unrelated to GSI functionality.
Final Answer:
To allow querying the table using alternative key attributes -> Option C
Quick Check:
GSI = alternative query keys [OK]
Hint: GSIs let you query by new keys without changing the table [OK]
Common Mistakes:
Thinking GSI increases storage size
Confusing GSI with backup or encryption features
Assuming GSI changes the primary key
2. Which of the following is the correct syntax snippet to define a GSI named OrderDateIndex with OrderDate as the partition key in a DynamoDB table creation JSON?
B. NonKeyAttributes must be a non-empty array when ProjectionType is INCLUDE
C. KeyType must be RANGE for GSIs
D. ProjectionType cannot be INCLUDE for GSIs
Solution
Step 1: Check ProjectionType and NonKeyAttributes
When ProjectionType is INCLUDE, NonKeyAttributes must be a non-empty array listing attributes to project.
Step 2: Verify given snippet
NonKeyAttributes is [], which is empty and invalid when ProjectionType is INCLUDE.
Step 3: Eliminate other options
ProjectionType INCLUDE is valid; KeyType HASH is valid for partition key; IndexName can contain 'Index'.
Final Answer:
NonKeyAttributes must be a non-empty array when ProjectionType is INCLUDE -> Option B
Quick Check:
INCLUDE projection requires NonKeyAttributes [OK]
Hint: INCLUDE projection needs NonKeyAttributes array [OK]
Common Mistakes:
Using empty or missing NonKeyAttributes with INCLUDE
Confusing KeyType HASH and RANGE for partition key
Thinking IndexName naming causes errors
5. You have a DynamoDB table with primary key UserID. You want to query users by their City and SignupDate. Which GSI definition allows efficient queries for all users in a city sorted by signup date?