0
0
DynamoDBquery~10 mins

Local Secondary Index (LSI) concept in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Local Secondary Index (LSI) concept
Create Table with Partition Key
Add Sort Key to Table
Define LSI with Same Partition Key + Different Sort Key
Insert Items into Table
Query Table Using LSI
Return Items Sorted by LSI Sort Key
LSI lets you query items with the same partition key but sorted by a different sort key defined at table creation.
Execution Sample
DynamoDB
CREATE TABLE Music (
  Artist STRING,
  AlbumTitle STRING,
  SongTitle STRING,
  Year NUMBER,
  PRIMARY KEY (Artist, AlbumTitle),
  LOCAL SECONDARY INDEX YearIndex (Artist, Year)
);

-- Query using LSI
SELECT * FROM Music WHERE Artist = 'Adele' ORDER BY Year;
Create a table with Artist as partition key and AlbumTitle as sort key, add LSI on Year, then query by Artist sorted by Year.
Execution Table
StepActionInputIndex UsedOutput RowsNotes
1Create TableArtist, AlbumTitle PK; Year LSINoneTable createdTable with LSI defined
2Insert Item{Artist:'Adele', AlbumTitle:'25', SongTitle:'Hello', Year:2015}NoneItem storedStored with PK and LSI keys
3Insert Item{Artist:'Adele', AlbumTitle:'21', SongTitle:'Someone Like You', Year:2011}NoneItem storedStored with PK and LSI keys
4Query TableArtist='Adele'YearIndex (LSI)2 rowsItems returned sorted by Year (2011, 2015)
5Query TableArtist='Adele' ORDER BY AlbumTitlePrimary Index2 rowsItems returned sorted by AlbumTitle
6Query TableArtist='Beyonce'YearIndex (LSI)0 rowsNo items for this artist
7EndNo more queriesNoneExecution stopsNo more data to fetch
💡 No more queries or data to fetch, execution ends.
Variable Tracker
VariableStartAfter Insert 1After Insert 2After Query 1After Query 2Final
Table Rows01 (Adele, 25, 2015)2 (Adele, 21, 2011)2 rows (Adele by Year)2 rows (Adele by AlbumTitle)2 rows
Key Moments - 3 Insights
Why does the LSI use the same partition key but a different sort key?
Because LSI allows querying items with the same partition key but sorted or filtered by a different attribute (sort key). See execution_table rows 4 and 5 where queries use the same Artist but different sort keys.
Can I add an LSI after the table is created?
No, LSIs must be defined when creating the table. This is shown in execution_table row 1 where the table is created with the LSI.
Why do queries using the LSI return sorted results by Year?
Because the LSI's sort key is Year, so querying via the LSI returns items sorted by Year automatically, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are items returned sorted by the LSI sort key?
AStep 4
BStep 5
CStep 2
DStep 3
💡 Hint
Check the 'Index Used' and 'Output Rows' columns in execution_table row 4.
According to variable_tracker, how many rows are in the table after the second insert?
A0
B1
C2
D3
💡 Hint
Look at the 'After Insert 2' column for 'Table Rows' in variable_tracker.
If you query for Artist='Beyonce' using the LSI, what is the expected output?
A2 rows
B0 rows
C1 row
DError
💡 Hint
See execution_table row 6 for query results with Artist='Beyonce'.
Concept Snapshot
Local Secondary Index (LSI):
- Defined at table creation
- Uses same partition key as table
- Has different sort key
- Enables queries sorted by alternate attribute
- Useful for multiple query patterns on same partition key
Full Transcript
Local Secondary Index (LSI) in DynamoDB lets you query items sharing the same partition key but sorted by a different sort key defined when creating the table. You create a table with a partition key and a sort key, then define an LSI with the same partition key but a different sort key. When you insert items, they are stored with both keys. Queries using the LSI return items sorted by the LSI's sort key. LSIs must be defined at table creation and cannot be added later. This allows flexible querying on the same partition key with different sorting options.