Reputation: 631
From the DynamoDB documentation:
Global secondary index — an index with a partition key and a sort key that can be different from those on the base table. A global secondary index is considered "global" because queries on the index can span all of the data in the base table, across all partitions.
Local secondary index — an index that has the same partition key as the base table, but a different sort key. A local secondary index is "local" in the sense that every partition of a local secondary index is scoped to a base table partition that has the same partition key value.
This just isn't making sense to me and no amount of searches is able to aptly explain it to me.
Could someone help me w/understanding this?
Upvotes: 8
Views: 3512
Reputation: 1071
Local secondary index(LSI)
table
capacity units
with a table
index
's partition key
has to be the same as table
's partition key
table
can have 5 LSI
Global secondary index(GSI)
table
items into index table
, it cost read capacity units
of table
)capacity unit
attribute
can be the partition key
table
can have 5 GSI
Upvotes: 7
Reputation: 19728
When you insert data to DynamoDB, it internally partitions the data and store in different storage nodes internally. This is based on the Partition Key.
Lets say you want to Query an Item based on a non-key (Neither partition nor sort key) attribute you need to use a Scan (Which is expensive since it checks all the items in the table).
This is where GSI snd LSI comes in. Lets take an example of a Student table with StudentsId as sort key and SchoolId as partition key.
LSI is useful if your application have queries like getting all the students of grade 5 of a given school.
If you need to query all grade 5 students across all the schools (Across all school partitions) you will need a GSI.
Upvotes: 7