Skip to content
Related Articles

Related Articles

Improve Article
AWS DynamoDB – Working with Indexes
  • Last Updated : 19 Apr, 2021

An index is a data structure that enables us to perform fast queries on different columns in a table. After creating an index, the database handles it for us. Whenever data is modified in the table, the index is automatically modified to reflect changes in the table. We can create and use a secondary index to query faster. While creating a secondary index, we must specify its key attributes—a partition key and a sort key. After the secondary index is created, we can perform operations such as Query or Scan just as we do on the table. DynamoDB doesn’t have any query optimizer, so a secondary index is used while you Query it or Scan it. DynamoDB supports two types of indexes which have been discussed below in detail:

Global Secondary Index:

A Global Secondary Index (GSI) is an index with a partition key and a sort key that can be different from keys in the base table. A global secondary index is said t be “global” because queries on the index can cover all the data in the base table, across all partitions. A global secondary index has no size limitations like that of the base table and has its own provisioned read and write throughput settings that are separate from those of the table. Therefore, for fast retrieval of results on non-key attributes, we use Secondary Global Indexes. See the below example:

BoardExams

A table named BoardExams has been created with partition key as StudentID and sort key as Stream. The data stored in the above table will be represented as shown in the below image:

Representation of Base table

Now, suppose we need to know the maths marks scored in each stream. Thus, we create a GSI named Stream-index with partition key as Stream and sort key as Maths. See the below image:

Note: The partition key of the base table will also be projected into the index. 

Stream-index

Local Secondary Index:

A Local Secondary Index has the same partition key as the base table but a different sort key. It 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. It is created when a table is created. See the below image:

My Personal Notes arrow_drop_up
Recommended Articles
Page :