We use Querying GSI to quickly find items in a DynamoDB table using an alternate key. It helps when you want to search by something other than the main key.
Querying GSI in DynamoDB
Introduction
Syntax
DynamoDB
aws dynamodb query \ --table-name TableName \ --index-name GSIName \ --key-condition-expression "GSIPartitionKey = :value" \ --expression-attribute-values '{":value":{"S":"YourValue"}}'
The --index-name option tells DynamoDB which GSI to use.
The --key-condition-expression filters items by the GSI partition key.
Examples
DynamoDB
aws dynamodb query \ --table-name Orders \ --index-name CustomerIndex \ --key-condition-expression "CustomerId = :cid" \ --expression-attribute-values '{":cid":{"S":"C123"}}'
DynamoDB
aws dynamodb query \ --table-name Products \ --index-name CategoryIndex \ --key-condition-expression "Category = :cat" \ --expression-attribute-values '{":cat":{"S":"Books"}}'
Sample Program
This query finds all music items where the artist is 'The Beatles' using the ArtistIndex GSI.
DynamoDB
aws dynamodb query \ --table-name Music \ --index-name ArtistIndex \ --key-condition-expression "Artist = :artist" \ --expression-attribute-values '{":artist":{"S":"The Beatles"}}'
Important Notes
Make sure the GSI is created before querying it.
You can only query by the GSI's partition key and optionally its sort key.
Querying a GSI is faster than scanning the whole table.
Summary
Querying a GSI lets you find items using an alternate key.
You specify the GSI name and key condition in the query.
This helps you search data in different ways without scanning the whole table.
Practice
1. What is the main purpose of querying a Global Secondary Index (GSI) in DynamoDB?
easy
Solution
Step 1: Understand what a GSI is used for
A GSI allows querying data using a different key than the primary key.Step 2: Identify the main benefit of querying a GSI
Querying a GSI helps find items quickly without scanning the entire table.Final Answer:
To find items using an alternate key without scanning the whole table -> Option DQuick Check:
Query GSI = alternate key search [OK]
Hint: GSI lets you search by different keys fast [OK]
Common Mistakes:
- Confusing GSI query with table scan
- Thinking GSI is for deleting or updating keys
- Assuming GSI creates backups
2. Which of the following is the correct way to specify a GSI name in a DynamoDB query using AWS SDK?
easy
Solution
Step 1: Recall the correct parameter name for GSI in DynamoDB query
The correct parameter to specify a GSI is 'IndexName'.Step 2: Check the options for correct syntax
Only query({ TableName: 'MyTable', IndexName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } }) uses 'IndexName' correctly with other required fields.Final Answer:
query({ TableName: 'MyTable', IndexName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } }) -> Option CQuick Check:
Use IndexName to specify GSI [OK]
Hint: Use 'IndexName' to specify GSI in query params [OK]
Common Mistakes:
- Using 'KeyName' or 'GSIName' instead of 'IndexName'
- Omitting ExpressionAttributeValues
- Wrong key condition syntax
3. Given a DynamoDB table with a GSI named 'StatusIndex' on attribute 'status', what will the following query return?
query({ TableName: 'Orders', IndexName: 'StatusIndex', KeyConditionExpression: 'status = :s', ExpressionAttributeValues: { ':s': 'shipped' } })medium
Solution
Step 1: Understand the query parameters
The query uses 'StatusIndex' GSI with a key condition on 'status' = 'shipped'.Step 2: Determine what the query returns
It returns all items where 'status' attribute equals 'shipped' using the GSI, which is designed for this attribute.Final Answer:
All orders with status 'shipped' using the GSI -> Option AQuick Check:
Query GSI by status = shipped returns matching items [OK]
Hint: Query GSI with key condition returns matching items [OK]
Common Mistakes:
- Thinking GSI query returns all items
- Assuming GSI query causes error if attribute not primary key
- Believing GSI queries are unsupported
4. You wrote this DynamoDB query to use a GSI but get an error:
What is the most likely cause of the error?
query({ TableName: 'Products', IndexName: 'CategoryIndex', KeyConditionExpression: 'category = :c', ExpressionAttributeValues: { ':c': 'books' } })What is the most likely cause of the error?
medium
Solution
Step 1: Understand GSI key requirements
When querying a GSI, the KeyConditionExpression must use the GSI's partition key attribute.Step 2: Identify the error cause
If 'category' is not the partition key of 'CategoryIndex', the query will fail with an error.Final Answer:
The attribute 'category' is not the partition key of the GSI 'CategoryIndex' -> Option BQuick Check:
GSI query key must be GSI partition key [OK]
Hint: GSI query key must match GSI partition key [OK]
Common Mistakes:
- Using a non-key attribute in KeyConditionExpression
- Assuming table name typo causes this error
- Believing GSI queries are unsupported
5. You have a DynamoDB table 'Employees' with a GSI named 'DeptIndex' where the partition key is 'department' and sort key is 'hireDate'. You want to find all employees in 'Sales' hired after '2023-01-01'. Which query is correct?
hard
Solution
Step 1: Use the GSI name and correct key condition
Query must specify 'IndexName' as 'DeptIndex' and use KeyConditionExpression with partition key and sort key conditions.Step 2: Check logical operators and expressions
KeyConditionExpression supports 'AND' for partition key and sort key conditions; 'OR' is invalid here.Step 3: Verify FilterExpression usage
FilterExpression filters after query; it cannot replace KeyConditionExpression for keys.Final Answer:
query({ TableName: 'Employees', IndexName: 'DeptIndex', KeyConditionExpression: 'department = :d AND hireDate > :h', ExpressionAttributeValues: { ':d': 'Sales', ':h': '2023-01-01' } }) -> Option AQuick Check:
Use KeyConditionExpression with AND on GSI keys [OK]
Hint: Use AND in KeyConditionExpression for partition and sort keys [OK]
Common Mistakes:
- Using OR instead of AND in KeyConditionExpression
- Omitting IndexName for GSI query
- Using FilterExpression instead of KeyConditionExpression for keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
