DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Amazon Dynamo DB Connector Operations Walkthrough in Mule 4, Part 1
  • Comparing SQL and SPL: Order-Based Computations
  • The Aggregate Reference Problem
  • The Serverless Ceiling: Designing Write-Heavy Backends With Aurora Limitless

Trending

  • Docker Hardened Images Are Free Now — Here's What You Still Need to Build
  • S3 Vectors: How to Build a RAG Without a Vector Database
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  1. DZone
  2. Data Engineering
  3. Databases
  4. Indexing in DynamoDB

Indexing in DynamoDB

DynamoDB provides fast access to items in a table by specifying primary key values. Indexing comes into the picture if you want to fetch the data of attributes other than the primary key.

By 
Shubham Agarwal user avatar
Shubham Agarwal
·
Apr. 11, 18 · Analysis
Likes (3)
Comment
Save
Tweet
Share
41.8K Views

Join the DZone community and get the full member experience.

Join For Free

Hello everyone! In this article, I will try to explain indexing in DynamooDb.

What Is Indexing in DynamoDB?

Amazon DynamoDB provides fast access to items in a table by specifying primary key values. But if you want to fetch the data of attributes other than the primary key, indexing comes into the picture.

DynamoDb provides two types of indexing:

  1. Global secondary index

  2. Local secondary index

When you create a secondary index, you need to specify the attributes that will be projected into the index. DynamoDB provides three different options for this:

  1. KEYS_ONLY: Each item in the index consists only of the table partition key and sort key values, plus the index key values. The KEYS_ONLY option results in the smallest possible secondary index.

  2. INCLUDE: In addition to the attributes described in KEYS_ONLY, the secondary index will include other non-key attributes that you specify.

  3. ALL: The secondary index includes all of the attributes from the source table. Because all of the table data is duplicated in the index, an ALL projection results in the largest possible secondary index.

Global Secondary Index in DynamoDB

This is an index with a partition key and a sort key that can be different from the base table. A global secondary index is very helpful when you need to query your data without a primary key.

  • The primary key of a global secondary index can be partition key or composite (partition key and sort key).
  • Global secondary indexes can be created at the same time that you create a table. You can also add a new global secondary index to an existing table or delete an existing global secondary index
  • A global secondary index lets you query over the entire table and across all partitions.
  • The index partition key and sort key (if present) can be any base table attributes of type string, number, or binary.
  • With global secondary index queries or scans, you can only request the attributes that are projected into the index. DynamoDB will not fetch any attributes from the table.
  • There are no size restrictions for global secondary indexes.

Let's create a global secondary index using CLI on the local index:

aws dynamodb create-table \
             --region=eu-west-1 \
             --endpoint-url http://localhost:8000 \
             --table-name users \
             --attribute-definitions \
                 AttributeName=id,AttributeType=S \
                 AttributeName=name,AttributeType=S \
                 AttributeName=age,AttributeType=S \
             --key-schema \
                 AttributeName=id,KeyType=HASH \
                 AttributeName=name,KeyType=RANGE \
             --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \
             --global-secondary-indexes IndexName=Index,\
KeySchema=["{AttributeName=name,KeyType=HASH}","{AttributeName=id,KeyType=RANGE}"],\
Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["age"]}",\
ProvisionedThroughput="{ReadCapacityUnits=10,WriteCapacityUnits=10}"

Here, you can see that we created the global secondary index with the table users. If you will see table than in this table we have:

  • Hash key: id

  • Range key: name

But in the global secondary index, we changed it, and in the global secondary index, we have:

  • Hash key: name

  • Range key: id

So, when your query is not able to satisfy your table condition (primary key), you can create the global secondary index and easily search your data.

Local Secondary Index in DynamoDB

This is an index that has the same partition key as the base table, but a different sort key.

Some applications only need to query data using the base table's primary key; however, there may be situations where an alternate sort key would be helpful. To give your application a choice of sort keys, you can create one or more local secondary indexes on a table and issue query or scan requests against these indexes.

Every local secondary index automatically contains the partition and sort keys from its base table. You can optionally project non-key attributes into the index. When you query the index, DynamoDB can retrieve these projected attributes efficiently. When you query a local secondary index, the query can also retrieve attributes that are not projected into the index. DynamoDB will automatically fetch these attributes from the base table but at a greater latency and with higher provisioned throughput costs.

  • The primary key of a local secondary index must be composite (partition key and sort key).
  • Local secondary indexes are created at the same time that you create a table. You cannot add a local secondary index to an existing table, nor can you delete any local secondary indexes that currently exist.
  • When you query a local secondary index, you can choose either eventual consistency or strong consistency.
  • If you query or scan a local secondary index, you can request attributes that are not projected into the index. DynamoDB will automatically fetch those attributes from the table.
  • Queries or scans on a local secondary index consume read capacity units from the base table. When you write to a table, its local secondary indexes are also updated; these updates consume write capacity units from the base table.

Now, let's create the local secondary index with users table:

aws dynamodb create-table \
             --region=eu-west-1 \
             --endpoint-url http://localhost:8000 \
             --table-name users \
             --attribute-definitions \
                 AttributeName=id,AttributeType=S \
                 AttributeName=name,AttributeType=S \
                 AttributeName=age,AttributeType=S \
             --key-schema \
                 AttributeName=id,KeyType=HASH \
                 AttributeName=name,KeyType=RANGE \
             --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \
             --local-secondary-indexes IndexName=localIndex,\
KeySchema=["{AttributeName=id,KeyType=HASH}","{AttributeName=age,KeyType=RANGE}"],\
Projection="{ProjectionType=INCLUDE ,NonKeyAttributes=["age"]}"

Here, you can see that we created the local secondary index where we have the same hash key (id) but different range key (age).

Database Attribute (computing) Sort (Unix) Relational database

Published at DZone with permission of Shubham Agarwal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Amazon Dynamo DB Connector Operations Walkthrough in Mule 4, Part 1
  • Comparing SQL and SPL: Order-Based Computations
  • The Aggregate Reference Problem
  • The Serverless Ceiling: Designing Write-Heavy Backends With Aurora Limitless

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook