0
0
DynamoDBquery~10 mins

Why secondary indexes enable flexible queries in DynamoDB - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why secondary indexes enable flexible queries
Start with main table query
Check if query matches primary key
Query main table
Return results
End
Queries first check if they can use the main table's primary key. If not, they use a secondary index to find data flexibly.
Execution Sample
DynamoDB
Query main table by primary key
If no match, query secondary index
Return matching items
This shows how a query tries the main table first, then uses a secondary index if needed.
Execution Table
StepQuery TargetConditionActionResult
1Main TableQuery on primary key attributeCheck main table for matching itemsFound 2 items
2Main TableQuery on non-primary key attributeNo match on primary key0 items found
3Secondary IndexQuery on non-primary key attributeCheck secondary index for matching itemsFound 3 items
4Secondary IndexQuery on another attribute not in indexNo matching index, no results0 items found
5EndNo more optionsReturn results from last successful queryQuery ends
💡 Query ends after checking main table and secondary index for matching items.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Query TargetMain TableMain TableMain TableSecondary IndexSecondary IndexSecondary Index
Items Found020303
Key Moments - 3 Insights
Why does the query check the main table first before using a secondary index?
Because the main table's primary key is the fastest way to find data. If the query matches the primary key, it returns results immediately (see execution_table step 1).
What happens if the query condition is on an attribute not in the primary key?
The main table query finds no items (step 2), so the query uses a secondary index that includes that attribute to find matching items (step 3).
Can a secondary index be used for any attribute?
No, only attributes included in the secondary index can be queried efficiently. If the attribute is not in the index, no results are found (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many items are found when querying the main table by primary key?
A0 items
B3 items
C2 items
D5 items
💡 Hint
Check execution_table row 1 under 'Result' column.
At which step does the query switch to using the secondary index?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Query Target' column in execution_table to see when it changes to 'Secondary Index'.
If the attribute is not in any index, what is the result of the query?
AReturns items from secondary index
BReturns zero items
CReturns items from main table
DReturns all items
💡 Hint
See execution_table step 4 where no matching index means zero items found.
Concept Snapshot
Secondary indexes let you query data by attributes other than the primary key.
Queries first try the main table's primary key.
If no match, they use secondary indexes for flexible queries.
Only attributes in the index can be queried efficiently.
This enables faster, flexible data retrieval in DynamoDB.
Full Transcript
This visual execution shows how DynamoDB queries use secondary indexes to enable flexible queries. The query first tries the main table using the primary key. If the query condition matches the primary key, it returns results immediately. If not, the query tries a secondary index that includes the attribute being queried. If the attribute is in the secondary index, matching items are found and returned. If the attribute is not in any index, no items are found. This process allows queries on different attributes efficiently, enabling flexible data retrieval beyond the primary key.