

With this base table key schema, it can answer queries to retrieve data for a uuid. GSI ExampleĬonsider this table that contains Uuid as primary key, UserId, and Data attributes. Global Secondary Indexes are sparse indexes as only specified attributes of the items in the base table appear in the index.Ĭheck out the following GSI and LSI examples to get an idea of when to use which.You can always create a new index that projects more attributes and replace the existing one when your use case changes. Since Global Secondary Indexes have their own throughput consumption, to minimize cost, I suggest project only attributes that are needed.The local secondary index allows Query operation to retrieve several items that have the same partition key value but different sort key values AND one item with a specific partition key value and a sort key value.

It enables data queries with different sorting orders of the specified sort key attribute. It is considered "local" because every partition of a local secondary index is bounded by the same partition key value of the base table. It can be viewed as a different table that contains attributes based on the base table.Ī local secondary index is an index that must have the same partition key but a different sort key from the base table. It is deemed "global" because queries on the index can access the data across different partitions of the base table. Global(GSI) vs Local Secondary Indexes(LSI)ĪWS DynamoDB supports two types of indexes: Global Secondary Index (GSI) and Local Secondary Index (LSI).Ī global secondary index is an index that has a partition key and an optional sort key that are different from the base table's primary key. When items are added, modified, or deleted in the base table, associated secondary indexes will be updated to reflect the changes. This other table is called a secondary index and is managed by AWS DynamoDB. Thus, there is a need for another table or data structure that stores data with different primary keys and maps a subset of attributes from this base table. Imagine, you have to look for a book in a library by going through possibly all the books in the library versus you knowing which shelf the book is at. However, scan operations access every item in a table which is slower than query operations that access items at specific indices. It is possible to obtain the same query result using the DynamoDB scan operation. WHERE that when doing the query above with an SQL database, a query optimizer evaluates available indexes to see if any index can fulfill the query. AWS DynamoDB being a No SQL database doesn’t support queries such as SELECT with a condition such as the following query.
