DynamoDB: How different indexes could be

Deepti Mittal
3 min readMay 11, 2023

--

Whenever anyone talk about databases the discussion is not over without talking about indexes. While you will find so many different things in dynamoDB than any other databases, but something which surprised me the most was how indexes are meant to work in dynamo DB.

Dynamo DB has 2 kinds of indexes.

Local secondary index: An index which needs to be created during table creation and cannot be added, modified or deleted later. Its called local as it uses same partition key as main table and can have different sort key.

Key Points

  • Unlike main table combination of partition and sort key does not need to be unique.
  • Sort key of main table becomes non-key attribute in index.
  • It contains it own copy of data where you can specify which all attributed needs to be projected.
  • One table can have upto 5 local secondary indexes.
  • It is useful to query data where particular partition data needs to be queried in different way, can be very useful to support different sorting criteria.
  • Consider projecting only required attributes to save storage, read and write cost. This will help in lower latency as well.
  • If your application frequently accesses some non-key attributes, you should consider projecting those attributes into a local secondary index. The additional storage costs for the local secondary index offset the cost of performing frequent table scans.
  • The GetItem and BatchGetItem operations can’t be used on a local secondary index.

Global secondary index(GSI): GSI can be defined during table creation and can be added, modified or deleted at any point of time. Like LSI it also maintains its own copy of data.

Key Points

  • It needs to have partition key but sort key is optional.
  • Unlike main table combination of partition and sort key does not need to be unique.
  • It contains it own copy of data where you can specify which all attributed needs to be projected.
  • The main usage of GSI is to support different access patterns than main table.
  • If your application frequently accesses some non-key attributes, you should consider projecting those attributes into a local secondary index. The additional storage costs for the local secondary index offset the cost of performing frequent table scans.

Difference between LSI and GSI

Difference between LSI and GSI

In DynamoDB indexes are more or less like new table with its own storage space and primary key but dynamoDB takes care of syncing data across main tables and indexes without any extra effort needed.

Its also important to keep in mind what can be done during table creation and what can be changed later as having unnecessary indexes going to make write slow for the main table and also going to impact read and write capacity for the main table leading to throttle exceptions.

Check out our other blogs in this series to understand concepts in details

dynamodb-all-you-need-to-know-about-it

data-partitioning-is-the-secret-sauce

provisioned-vs-on-demand-capacity

demystifying-dynamodb-partition-keys

sort-key

unlocking-efficient-data-retrieval-in-dynamodb

pagination-in-dynamodb-efficient-data-reading

how-simple-and-complex-is-write-operation

--

--

Deepti Mittal

I am working as software engineer in Bangalore for over a decade. Love to solve core technical problem and then blog about it.