Course contentsShow
← Cloud and Infrastructure
Lesson 433 of 2,9898. Managed DatabasesFree lesson

DynamoDB Secondary Indexes: GSI and LSI

Creating and using Global Secondary Indexes and Local Secondary Indexes for flexible query patterns.

DynamoDB Secondary Indexes: GSI and LSI

You'll learn how to create alternate query paths in DynamoDB using Global and Local Secondary Indexes for flexible data access patterns.

Why Secondary Indexes Matter

By default, DynamoDB only lets you query by your primary key (partition key or partition + sort key). But what if you need to find items by different attributes? That's where secondary indexes come in—they create alternate "views" of your data optimized for different queries.

Two Types of Secondary Indexes

Local Secondary Index (LSI)

A Local Secondary Index shares the same partition key as your base table but uses a different sort key. Think of it like sorting your bookshelf differently—same books (same partition), new order.

  • Created only at table creation time
  • Limited to 5 per table
  • Shares throughput capacity with the base table
  • Always strongly consistent reads available

Example use case: A table partitioned by UserId with sort key OrderDate could have an LSI with sort key OrderStatus to quickly find all orders for a user by status.

Global Secondary Index (GSI)

A Global Secondary Index can use completely different partition and sort keys from your base table. It's like creating an entirely new organizational system for the same data.

  • Can be created or deleted anytime
  • Has its own separate throughput capacity
  • Eventually consistent reads (slight delay possible)
  • More flexible but costs more

Example use case: Query orders by ProductId instead of UserId—totally different access pattern.

Choosing Between Them

Use LSI when you need different sorting within the same partition. Use GSI when you need entirely different query dimensions.

Key Takeaway: Secondary indexes (GSI and LSI) enable flexible query patterns in DynamoDB by creating alternate key structures—LSIs for different sorting within partitions, GSIs for completely new access patterns.