What is Secondary Index in DynamoDB: Explanation and Example
secondary index in DynamoDB is a way to create an alternate view of your data to query it efficiently using different keys than the primary key. It lets you search the table by attributes other than the main key without scanning the entire table.How It Works
Think of a DynamoDB table like a big phone book sorted by last name (the primary key). If you want to find someone by their phone number instead, you would have to look through every page, which is slow. A secondary index is like having a separate mini phone book sorted by phone numbers, so you can quickly find the person using that attribute.
Technically, a secondary index creates a copy of selected attributes from the main table, organized by a different key you choose. This lets DynamoDB quickly find items based on that key without scanning the whole table. There are two types: Global Secondary Index (GSI) which can use any attribute as a key, and Local Secondary Index (LSI) which shares the same partition key but uses a different sort key.
Example
This example shows how to create a Global Secondary Index on a DynamoDB table to query by a different attribute.
import boto3 dynamodb = boto3.resource('dynamodb') # Create table with primary key 'UserId' # and a Global Secondary Index on 'Email' table = dynamodb.create_table( TableName='Users', KeySchema=[ {'AttributeName': 'UserId', 'KeyType': 'HASH'} # Partition key ], AttributeDefinitions=[ {'AttributeName': 'UserId', 'AttributeType': 'S'}, {'AttributeName': 'Email', 'AttributeType': 'S'} ], GlobalSecondaryIndexes=[ { 'IndexName': 'EmailIndex', 'KeySchema': [ {'AttributeName': 'Email', 'KeyType': 'HASH'} ], 'Projection': { 'ProjectionType': 'ALL' }, 'ProvisionedThroughput': { 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } } ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } ) table.wait_until_exists() print('Table and GSI created successfully')
When to Use
Use a secondary index when you need to query your DynamoDB table by attributes other than the primary key. For example, if your table's primary key is UserId but you want to find users by their Email or PhoneNumber, a secondary index lets you do this efficiently.
Secondary indexes are useful in real-world cases like searching customers by email, orders by status, or products by category without scanning the entire table, which saves time and cost.
Key Points
- A secondary index provides an alternate way to query data in DynamoDB.
- Global Secondary Index (GSI) can use any attribute as a key.
- Local Secondary Index (LSI) shares the partition key but uses a different sort key.
- Indexes improve query speed but add some storage and write cost.
- Use indexes to avoid slow full table scans when querying by non-primary keys.