0
0
DynamoDBquery~20 mins

Global Secondary Index (GSI) concept in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GSI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of a Global Secondary Index

What is the main purpose of creating a Global Secondary Index (GSI) in DynamoDB?

ATo allow querying the table using an alternate key with different partition and sort keys
BTo increase the storage capacity of the main table automatically
CTo create a backup of the table data in another region
DTo enforce stricter data validation rules on the main table
Attempts:
2 left
💡 Hint

Think about why you might want to search your data in a different way than the main key.

query_result
intermediate
2:00remaining
Querying with a Global Secondary Index

Given a DynamoDB table Orders with a GSI named CustomerIndex that uses CustomerID as the partition key, what will the following query return?

Query on CustomerIndex where CustomerID = 'C123'
AAll orders placed by customer with ID 'C123'
BAll orders sorted by order date for all customers
CAll orders with order ID 'C123'
DAn error because <code>CustomerID</code> is not the primary key of the main table
Attempts:
2 left
💡 Hint

Remember that a GSI lets you query by the alternate key defined in the index.

📝 Syntax
advanced
2:00remaining
Identifying correct GSI creation syntax

Which of the following JSON snippets correctly defines a Global Secondary Index named GSI1 with Category as the partition key and Price as the sort key?

A{ "IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "RANGE"}, {"AttributeName": "Price", "KeyType": "HASH"}], "Projection": {"ProjectionType": "KEYS_ONLY"} }
B{ "IndexName": "GSI1", "KeySchema": [{"AttributeName": "Price", "KeyType": "HASH"}, {"AttributeName": "Category", "KeyType": "RANGE"}], "Projection": {"ProjectionType": "ALL"} }
C{ "IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "HASH"}, {"AttributeName": "Price", "KeyType": "RANGE"}], "Projection": {"ProjectionType": "ALL"} }
D{ "IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category"}], "Projection": {"ProjectionType": "ALL"} }
Attempts:
2 left
💡 Hint

The partition key must have KeyType as HASH and the sort key as RANGE.

optimization
advanced
2:00remaining
Choosing attributes to project in a GSI

You want to optimize read performance and reduce storage costs for a GSI that queries only OrderID and OrderDate. Which projection type should you choose?

ANONE - no attributes are projected
BKEYS_ONLY - projects only the index keys
CALL - projects all attributes from the main table
DINCLUDE - projects only <code>OrderID</code> and <code>OrderDate</code>
Attempts:
2 left
💡 Hint

Think about projecting only the attributes you need to reduce storage and improve query speed.

🔧 Debug
expert
2:00remaining
Diagnosing a GSI query error

You run a query on a GSI named GSI2 with partition key UserID, but get the error: ValidationException: Query condition missed key schema element: UserID. What is the most likely cause?

AThe GSI <code>GSI2</code> does not exist on the table
BThe query did not specify the <code>UserID</code> value in the key condition expression
CThe main table does not have a <code>UserID</code> attribute
DThe query used the main table's primary key instead of the GSI
Attempts:
2 left
💡 Hint

Check if the query includes the partition key value required by the index.