0
0
DynamoDBquery~10 mins

Global Secondary Index (GSI) concept in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global Secondary Index (GSI) concept
Create Table with Primary Key
Add Global Secondary Index (GSI)
Write Data to Table
Data Stored in Table and GSI
Query Table using Primary Key
Return Matching Items
This flow shows how a DynamoDB table with a Global Secondary Index stores data and allows queries on both the primary key and the GSI key.
Execution Sample
DynamoDB
Create Table Users (UserID PK)
Add GSI on Email (Email as GSI PK)
Insert Item {UserID: 1, Email: 'a@x.com'}
Query Table by UserID = 1
Query GSI by Email = 'a@x.com'
This example creates a table with a primary key, adds a GSI on Email, inserts a user, then queries by both keys.
Execution Table
StepActionInputData Stored in TableData Stored in GSIQuery TypeQuery InputQuery Result
1Create TableUsers with PK UserIDEmptyEmptyN/AN/AN/A
2Add GSIGSI on EmailEmptyEmptyN/AN/AN/A
3Insert Item{UserID:1, Email:'a@x.com'}{UserID:1, Email:'a@x.com'}{Email:'a@x.com', UserID:1}N/AN/AN/A
4Query TableUserID=1{UserID:1, Email:'a@x.com'}{Email:'a@x.com', UserID:1}Primary KeyUserID=1{UserID:1, Email:'a@x.com'}
5Query GSIEmail='a@x.com'{UserID:1, Email:'a@x.com'}{Email:'a@x.com', UserID:1}GSI KeyEmail='a@x.com'{UserID:1, Email:'a@x.com'}
6Query TableUserID=2{UserID:1, Email:'a@x.com'}{Email:'a@x.com', UserID:1}Primary KeyUserID=2No items found
7Query GSIEmail='b@x.com'{UserID:1, Email:'a@x.com'}{Email:'a@x.com', UserID:1}GSI KeyEmail='b@x.com'No items found
💡 Queries stop when no matching items are found or all steps complete.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Table DataEmpty{UserID:1, Email:'a@x.com'}{UserID:1, Email:'a@x.com'}{UserID:1, Email:'a@x.com'}{UserID:1, Email:'a@x.com'}
GSI DataEmpty{Email:'a@x.com', UserID:1}{Email:'a@x.com', UserID:1}{Email:'a@x.com', UserID:1}{Email:'a@x.com', UserID:1}
Query ResultN/AN/A{UserID:1, Email:'a@x.com'}{UserID:1, Email:'a@x.com'}No items found
Key Moments - 3 Insights
Why does the data appear in both the table and the GSI after insertion?
Because DynamoDB automatically copies the indexed attributes to the GSI, so queries on the GSI can find the data quickly. See execution_table rows 3 and 5.
Can I query the table using the GSI key directly?
No, you must query the GSI itself using its key. The table query only works with the primary key. See execution_table rows 4 and 5.
What happens if I query a key that does not exist in the table or GSI?
The query returns no items found, as shown in execution_table rows 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what data is stored in the GSI after inserting the item?
A{UserID:1, Email:'a@x.com'}
BEmpty
C{Email:'a@x.com', UserID:1}
DNo data stored
💡 Hint
Check the 'Data Stored in GSI' column at Step 3 in the execution_table.
At which step does querying the table by UserID return no items?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look at the 'Query Result' column for queries by UserID in execution_table.
If we add another item with Email 'b@x.com', how would the GSI data change after insertion?
AIt would remain unchanged
BIt would include {Email:'b@x.com', UserID:2} alongside existing data
CIt would replace existing GSI data
DIt would delete the old data
💡 Hint
GSIs store indexed attributes for all items, so new items add to GSI data.
Concept Snapshot
Global Secondary Index (GSI) in DynamoDB:
- GSI is an alternate key for querying data.
- It has its own partition and sort keys.
- Data is copied automatically to GSI when inserted.
- Query GSI using its key, not the table's primary key.
- Useful for flexible queries without changing main table keys.
Full Transcript
This visual execution shows how a DynamoDB table with a Global Secondary Index (GSI) works. First, a table is created with a primary key. Then a GSI is added on a different attribute. When an item is inserted, its data is stored in the table and also copied to the GSI. Queries can be made using the table's primary key or the GSI key. Queries return matching items or no items if none match. This helps understand how GSIs enable querying data by alternate keys efficiently.