Bird
Raised Fist0
DynamoDBquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a Global Secondary Index (GSI) in DynamoDB?
easy
A. To allow queries using alternate keys different from the primary key
B. To store backup copies of the table data
C. To increase the size limit of the table
D. To encrypt data automatically

Solution

  1. Step 1: Understand what a GSI does

    A GSI lets you query the table using different keys than the main primary key.
  2. Step 2: Compare options to GSI purpose

    Options A, C, and D describe unrelated features like backup, size, or encryption, which are not GSI functions.
  3. Final Answer:

    To allow queries using alternate keys different from the primary key -> Option A
  4. Quick Check:

    GSI = alternate query keys [OK]
Hint: GSI lets you search by different keys, not backup or encryption [OK]
Common Mistakes:
  • Confusing GSI with backups
  • Thinking GSI increases table size
  • Assuming GSI encrypts data
2. Which of the following is the correct way to define a Global Secondary Index (GSI) in a DynamoDB table creation JSON?
easy
A. { "GlobalSecondaryIndexes": [ { "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "GSIKey", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "ALL"} } ] }
B. { "SecondaryIndexes": [ { "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "GSIKey", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "KEYS_ONLY"} } ] }
C. { "GlobalSecondaryIndexes": { "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "GSIKey", "KeyType": "HASH"} ], "Projection": {"ProjectionType": "ALL"} } }
D. { "GlobalSecondaryIndexes": [ { "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "GSIKey", "KeyType": "HASH"}, {"AttributeName": "SortKey", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "INCLUDE", "NonKeyAttributes": ["Attr1"]} } ] }

Solution

  1. Step 1: Check the correct JSON structure for GSI

    The GlobalSecondaryIndexes must be an array of objects, each with IndexName, KeySchema, and Projection.
  2. Step 2: Validate key schema and projection

    { "GlobalSecondaryIndexes": [ { "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "GSIKey", "KeyType": "HASH"}, {"AttributeName": "SortKey", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "INCLUDE", "NonKeyAttributes": ["Attr1"]} } ] } correctly uses an array, has both HASH and RANGE keys, and a valid projection with included attributes.
  3. Final Answer:

    Correct GSI JSON with array, keys, and projection -> Option D
  4. Quick Check:

    GSI JSON array with keys and projection = A [OK]
Hint: GSI must be an array with keys and projection defined [OK]
Common Mistakes:
  • Using object instead of array for GlobalSecondaryIndexes
  • Wrong key types or missing HASH key
  • Incorrect property names like SecondaryIndexes
3. Given a DynamoDB table with a GSI on attribute Category (HASH key) and Price (RANGE key), what will the following query return?

Query({ IndexName: 'CategoryPriceIndex', KeyConditionExpression: 'Category = :cat and Price > :minPrice', ExpressionAttributeValues: { ':cat': 'Books', ':minPrice': 20 } })
medium
A. All items where Price is greater than 20 regardless of Category
B. All items where Category is 'Books' or Price is greater than 20
C. All items where Category is 'Books' and Price is greater than 20
D. Syntax error because Price cannot be used in KeyConditionExpression

Solution

  1. Step 1: Understand KeyConditionExpression with GSI keys

    The query uses the GSI's HASH key 'Category' with equality and RANGE key 'Price' with a greater than condition, which is valid.
  2. Step 2: Interpret the query filter

    The query returns items where Category equals 'Books' AND Price is greater than 20, matching the GSI keys.
  3. Final Answer:

    All items where Category is 'Books' and Price is greater than 20 -> Option C
  4. Quick Check:

    GSI query with HASH and RANGE keys = All items where Category is 'Books' and Price is greater than 20 [OK]
Hint: GSI query uses HASH = and RANGE with conditions correctly [OK]
Common Mistakes:
  • Confusing AND with OR in KeyConditionExpression
  • Thinking Price can't be in KeyConditionExpression
  • Assuming query returns all items ignoring Category
4. You created a GSI with IndexName: 'UserEmailIndex' on attribute Email as HASH key. Your query on this GSI returns no results even though items with matching emails exist. What is the most likely cause?
medium
A. The GSI was created after the items were inserted and has not yet indexed them
B. You used the primary key instead of the GSI key in the query
C. The Email attribute is not projected to the GSI
D. DynamoDB does not support querying on GSIs

Solution

  1. Step 1: Understand GSI indexing timing

    When a GSI is created after data insertion, it takes time to index existing items. Queries may return empty until indexing completes.
  2. Step 2: Evaluate other options

    You used the primary key instead of the GSI key in the query is about query keys but question states query is on GSI. The Email attribute is not projected to the GSI is invalid because HASH key attributes must be projected automatically. DynamoDB does not support querying on GSIs is false; DynamoDB supports GSI queries.
  3. Final Answer:

    The GSI was created after the items were inserted and has not yet indexed them -> Option A
  4. Quick Check:

    New GSI indexing delay causes empty query [OK]
Hint: New GSI needs time to index existing data before queries work [OK]
Common Mistakes:
  • Assuming GSI queries always return instantly after creation
  • Confusing projection with key attributes
  • Believing DynamoDB doesn't support GSI queries
5. You have a DynamoDB table with a primary key UserID (HASH) and a GSI named OrderDateIndex with OrderDate as HASH and Status as RANGE key. You want to efficiently find all orders placed on '2023-05-01' with status 'Shipped'. Which query approach is best?
hard
A. Use a Scan on the GSI with FilterExpression for OrderDate and Status
B. Query the OrderDateIndex with KeyConditionExpression 'OrderDate = :date and Status = :status'
C. Query the main table with KeyConditionExpression 'UserID = :user and Status = :status'
D. Scan the table filtering for OrderDate and Status attributes

Solution

  1. Step 1: Identify the best index for the query

    The GSI OrderDateIndex has OrderDate as HASH and Status as RANGE key, matching the query conditions.
  2. Step 2: Choose efficient query method

    Querying the GSI with KeyConditionExpression on HASH and RANGE keys is efficient and uses indexes. Scans are less efficient and should be avoided.
  3. Final Answer:

    Query the OrderDateIndex with KeyConditionExpression 'OrderDate = :date and Status = :status' -> Option B
  4. Quick Check:

    Use GSI query with HASH and RANGE keys for best performance [OK]
Hint: Query GSI with HASH and RANGE keys, avoid scans for efficiency [OK]
Common Mistakes:
  • Using Scan instead of Query causing slow performance
  • Querying main table with wrong keys
  • Filtering after Scan instead of using KeyConditionExpression
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