Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Indexes (GSI/LSI) in DynamoDB

Introduction

Amazon DynamoDB is a fully managed NoSQL database that provides fast and predictable performance with seamless scalability. One of the key features that enhance the performance of query operations in DynamoDB is the use of indexes.

What are GSI and LSI?

Global Secondary Index (GSI)

A GSI allows you to query data on non-key attributes. You can create a GSI with a different partition key and sort key than the base table. This is particularly helpful for scenarios where you need to query data efficiently based on different access patterns.

Local Secondary Index (LSI)

An LSI allows you to create an index on a different sort key while using the same partition key as the base table. This is useful for querying items with the same partition key but different sort keys.

When to Use GSI and LSI?

Important Note: Choose GSI when you need a different partition key, and use LSI when you need to query on different sort keys while keeping the same partition key.
  • Use GSI when:
    • You require queries on non-key attributes.
    • You need to support multiple access patterns.
    • Your access patterns may change over time.
  • Use LSI when:
    • You want to query items that share the same partition key.
    • You need to retain the same partition key but require sorting by different attributes.

Best Practices

  1. Analyze query patterns before designing indexes.
  2. Limit the number of indexes to reduce costs and complexity.
  3. Monitor usage of indexes to optimize performance.
  4. Consider the write capacity of the table as GSIs consume additional write capacity.
  5. Use LSI when you have a fixed set of attributes that you need to query frequently.

FAQ

What is the difference between GSI and LSI?

GSI allows for different partition keys, while LSI is limited to the same partition key but allows different sort keys.

Can I create an index after the table is created?

Yes, you can create both GSIs and LSIs after the table is created, but it may impact performance during the index creation.

How many GSIs can I have on a table?

You can have up to 20 GSIs on a single DynamoDB table.

Code Example: Creating a GSI using AWS SDK for JavaScript


const AWS = require('aws-sdk');
const dynamoDB = new AWS.DynamoDB();

const params = {
    TableName: 'YourTableName',
    AttributeDefinitions: [
        { AttributeName: 'NewPartitionKey', AttributeType: 'S' },
        { AttributeName: 'NewSortKey', AttributeType: 'S' }
    ],
    GlobalSecondaryIndexes: [
        {
            IndexName: 'NewGSI',
            KeySchema: [
                { AttributeName: 'NewPartitionKey', KeyType: 'HASH' },
                { AttributeName: 'NewSortKey', KeyType: 'RANGE' }
            ],
            Projection: {
                ProjectionType: 'ALL'
            },
            ProvisionedThroughput: {
                ReadCapacityUnits: 5,
                WriteCapacityUnits: 5
            }
        }
    ],
};

dynamoDB.updateTable(params, function(err, data) {
    if (err) console.log("Error", err);
    else console.log("Success", data);
});