ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

Create Secondary Indexes In DynamoDB – AWS

Create Secondary Indexes In DynamoDB –  AWS
Create Secondary Indexes In DynamoDB – AWS

In this article, we will learn about how to create secondary indexes in AWS DynamoDB. In its Amazon Web Services portfolio, DynamoDB is a fully managed proprietary NoSQL database service that supports key-value and document data structures. In the DynamoDB table, each item is uniquely identified by a primary key. DynamoDB supports two types of primary keys: a simple primary key made up of just a partition key, and a composite primary key made up of a partition key and a sort key. These keys are used to perform the basic operation in dynamo DB. When performing multiple operations at once, it poses the threat that some operations may be completed and some may fail. Due to this incorrect data will be created, to solve this issue, Dynamo DB provided the transaction features.

secondary index is an object that contains a subset of data from a table as well as a different key for the purpose of supporting queries. In much the same way as a table, you can retrieve data from the index using a Query. Your applications can use multiple secondary indexes on a table, giving them access to a wide variety of query patterns. In DynamoDB, we can create 2 types of secondary indexes :

  • Local Secondary Indexes
  • Global Secondary Indexes

Local Secondary Indexes:- It uses the same partition key as their underlying tables but uses a different sort key.

Global Secondary Indexes:- It can use an entirely different primary key for a table, such as setting an index on just a partition key for a table with a composite primary key or populating a partition key and sort key with entirely different attributes. 

Prerequisites

  1. AWS account
  2. Basic NoSQL DynamoDB concepts

Create Secondary Indexes In AWS DynamoDB

Setup

  • Go to AWS Console and Log In
  • In the search bar, type dynamodb
  • Select the DynamoDB options displayed under Services section
  • After that DynamoDb service page will appear. Then click the Create table button on the right side of the page.
  • Enter the desired table name, partition key, and sort key for DynamoDB.
  • Under Table settings section, choose to customize settings for creating secondary indexes.
  • Then, Select Create global index to create Global Secondary Index.
  • Enter the partition and sort key for the secondary index which will be used to query the data.
  • Custom index names can be entered as desired to distinguish the indexes.
  • Click the Create index button to create the index.
  • The created index will be displayed under Secondary indexes the section.
  • Now, click Create table which will create a dynamoDB table with email-userid-index global secondary index.
create secondary indexes in aws

It will take some time to create a dynamoDB table. After creating the table will be listed under Table section.

Create a DynamoDB Item

  • For creating an item, select the user-table and click Create item option under Action.
  • Click Add new attribute and choose String option which will create a new option under createdAt field. Rename the attribute name to email.
  • Enter the userId, createdAt, and email attribute value as provided sample below:
  • userId:  1234-abc-2536-ran, createdAt: 20220820, email: test@test.com
  • Here, the value of the createdAt field is the UNIX timestamp.
  • After clicking, create an item a single item with the above value will be created.

Querying DynamoDB Using Secondary Index

In DynamoDB, we can query using the normal table or using the secondary index. For querying using a global secondary index created using the above method, follow the following step:

  • Select user-table .
  • Click the Query tab under Sca/Query Items section
  • Choose the index under the dropdown beside the query option
  • Under the email, section enter the value
  • Then, click Run.

The provided result will be all the records with email test@test.com.

In DynamoDB, the query operation accesses the data in the desired partition directly whereas the scan operation access all the data available in the table resulting in high process time and high cost.

Conclusion

In this article, we’ve learned how to create a secondary index in AWS DynamoDB and how to query the table items using a secondary index.

Sign up for daily dose of tech articles at your inbox.
Loading