3
votes

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?

2
Just a correction, Dynamo DB table can have 20 GSIs (default limit) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…Ashutosh

2 Answers

3
votes

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.

2
votes

Local secondary index(LSI)

  • can only be created when creating a table
  • share the capacity units with a table
  • index's partition key has to be the same as table's partition key
  • a table can have 5 LSI

Global secondary index(GSI)

  • can be created anytime but takes times to set one up (due to copying original table items into index table, it cost read capacity units of table)
  • have a separate set of capacity unit
  • any attribute can be the partition key
  • a table can have 5 GSI