Secondary indexes let you search your data in new ways without changing your main table. They make your queries more flexible and faster.
Why secondary indexes enable flexible queries in DynamoDB
CREATE GLOBAL SECONDARY INDEX index_name ON table_name (partition_key, sort_key); -- Or when creating a table: CREATE TABLE table_name ( primary_key_attribute TYPE, other_attribute TYPE, ... ) WITH GLOBAL SECONDARY INDEXES ( index_name (partition_key_attribute, sort_key_attribute) );
Secondary indexes can have their own partition and sort keys.
Global Secondary Indexes (GSI) allow querying by different keys than the main table.
CREATE TABLE Music ( Artist STRING, SongTitle STRING, AlbumTitle STRING, Year INT, PRIMARY KEY (Artist, SongTitle) ) WITH GLOBAL SECONDARY INDEXES ( AlbumIndex (AlbumTitle, Year) );
SELECT * FROM Music.AlbumIndex WHERE AlbumTitle = 'Greatest Hits';This example creates a Books table with ISBN as the main key. It adds a secondary index on Author and Title so you can find all books by a specific author quickly.
CREATE TABLE Books ( ISBN STRING, Title STRING, Author STRING, Genre STRING, PRIMARY KEY (ISBN) ) WITH GLOBAL SECONDARY INDEXES ( AuthorIndex (Author, Title) ); -- Insert sample data INSERT INTO Books VALUES ('123', 'Learn SQL', 'Alice', 'Education'); INSERT INTO Books VALUES ('456', 'Cooking 101', 'Bob', 'Cooking'); INSERT INTO Books VALUES ('789', 'Advanced SQL', 'Alice', 'Education'); -- Query by Author using secondary index SELECT * FROM Books.AuthorIndex WHERE Author = 'Alice';
Secondary indexes increase storage and write costs because data is duplicated.
They help avoid slow full table scans by enabling targeted queries.
Remember to choose index keys based on your query needs.
Secondary indexes let you query data in new ways without changing the main table.
They improve query speed and flexibility by using different keys.
Use them to support multiple search patterns efficiently.
Practice
Solution
Step 1: Understand the role of secondary indexes
Secondary indexes let you query the table using keys other than the primary key.Step 2: Compare other options
Options A, B, and C describe features unrelated to secondary indexes.Final Answer:
They allow querying data using different keys without changing the main table -> Option AQuick Check:
Secondary indexes = flexible queries [OK]
- Confusing secondary indexes with backups
- Thinking indexes increase storage size
- Assuming indexes encrypt data
Solution
Step 1: Identify correct KeyType for GSI partition key
For a GSI, the partition key uses KeyType 'HASH'. {"IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}} uses 'HASH'.Step 2: Check Projection type
{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}} uses 'ALL', which is valid. Other options use invalid KeyTypes like 'PRIMARY' or 'SORT'.Final Answer:
{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "Category", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}} -> Option BQuick Check:
GSI partition key = HASH [OK]
- Using 'PRIMARY' or 'SORT' as KeyType
- Confusing partition key with sort key
- Wrong ProjectionType values
Category, what will the following query return?Query({
TableName: 'Products',
IndexName: 'CategoryIndex',
KeyConditionExpression: 'Category = :cat',
ExpressionAttributeValues: { ':cat': 'Books' }
})Solution
Step 1: Understand the use of IndexName in the query
Specifying IndexName means the query uses the GSI on 'Category'.Step 2: Analyze KeyConditionExpression
The query filters items where 'Category' equals 'Books' using the GSI.Final Answer:
All items where the Category attribute equals 'Books' using the GSI -> Option DQuick Check:
Query with GSI filters by Category = 'Books' [OK]
- Assuming query filters primary key without IndexName
- Expecting error without primary key in main table
- Thinking query returns all items
ValidationException: Query condition missed key schema element. What is the most likely cause?Solution
Step 1: Understand the error message
The error means the query is missing a required key attribute from the index's key schema.Step 2: Identify the missing key
When querying a secondary index, you must specify its partition key attribute in the KeyConditionExpression.Final Answer:
You did not specify the partition key attribute of the secondary index in the query -> Option CQuick Check:
Missing index partition key causes ValidationException [OK]
- Using primary key attributes instead of index keys
- Querying before index creation
- Confusing Scan with Query operation
UserId and a Global Secondary Index (GSI) on Email. You want to efficiently find users by their email address. Which approach best uses the GSI to achieve this?Solution
Step 1: Identify the best way to find users by Email
The GSI on Email allows querying by Email efficiently without scanning.Step 2: Compare options
Query the table using the GSI with KeyConditionExpression 'Email = :emailValue' uses the GSI query correctly. Scan the entire table filtering items where Email equals the value scans, which is slow. Query the table using the primary key UserId with the email value uses UserId, which won't help find by Email. Create a new table with Email as the primary key is unnecessary duplication.Final Answer:
Query the table using the GSI with KeyConditionExpression 'Email = :emailValue' -> Option AQuick Check:
Use GSI query for efficient Email lookup [OK]
- Using Scan instead of Query on GSI
- Querying primary key with wrong attribute
- Creating redundant tables instead of indexes
Ready to prove it?
Pass the test and this pattern moves unknown
