Querying with Secondary Indexes: GSIs and LSIs
Learn how Global and Local Secondary Indexes let you query DynamoDB by attributes other than the primary key, and how to choose between them.
Querying with Secondary Indexes: GSIs and LSIs is a free Serverless Backend with AWS Lambda & API Gateway lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Serverless Backend with AWS Lambda & API Gateway learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Primary Key Limitation
DynamoDB queries efficiently only by the primary key (partition key, optionally plus sort key). To query by other attributes you need a secondary index.
- Scanning the whole table is slow and costly
- Indexes give you efficient alternate access paths
What Is a Secondary Index?
A secondary index is an alternate view of your table data, organized by a different key. DynamoDB keeps it in sync automatically as you write to the base table.
Global Secondary Index (GSI)
A GSI can use any attributes as its partition and sort key, different from the table key. It has its own throughput and can be created anytime.
- Query by a completely different attribute
- Spans all partitions of the table
- Eventually consistent reads only
Local Secondary Index (LSI)
An LSI shares the table partition key but uses a different sort key. It must be created at table creation time and supports strongly consistent reads.
GSI vs LSI at a Glance
Choosing between them:
- GSI: different partition key, created anytime, eventually consistent
- LSI: same partition key, created with the table, strongly consistent
Defining a GSI
You specify the index name and its key schema. Reads and writes against the index are billed separately from the base table.
aws dynamodb update-table --table-name Orders \
--attribute-definitions AttributeName=status,AttributeType=S \
--global-secondary-index-updates "[{...}]"Querying an Index
To query a GSI you pass its name and the index key condition. The query returns items matching that alternate key.
aws dynamodb query --table-name Orders \
--index-name status-index \
--key-condition-expression "status = :s" \
--expression-attribute-values '{":s":{"S":"PAID"}}'Projection: Which Attributes Are Copied
An index projection controls which attributes are copied into it: keys only, a selected set, or all. Projecting fewer attributes saves storage but may force a base-table fetch.
- KEYS_ONLY: smallest, just keys
- INCLUDE: keys plus chosen attributes
- ALL: full copy, biggest
Index Throughput and Cost
GSIs consume their own read and write capacity. Every base-table write that affects an indexed attribute also writes to the GSI, so over-indexing increases cost.
Sparse Indexes
If an item lacks the index key attribute, it is not written to the index. This creates a sparse index, an efficient way to query only items that have a certain attribute set.
Designing Access Patterns First
In DynamoDB you design indexes around your access patterns, not the other way around. List the queries your app needs, then create the minimal set of indexes that serve them.
Quick Check
Test your index knowledge.
Recap
You learned how secondary indexes enable querying DynamoDB by non-primary-key attributes. You compared GSIs (flexible keys, created anytime, eventually consistent) with LSIs (shared partition key, created with the table, strongly consistent), and covered projections, throughput, sparse indexes, and access-pattern-first design.
Frequently asked questions
Is the “Querying with Secondary Indexes: GSIs and LSIs” lesson free?
Yes — the full text of “Querying with Secondary Indexes: GSIs and LSIs” is free to read here on the web, and the Serverless Backend with AWS Lambda & API Gateway course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Serverless Backend with AWS Lambda & API Gateway course, upgrade to CoddyKit PRO.
What will I learn in “Querying with Secondary Indexes: GSIs and LSIs”?
Learn how Global and Local Secondary Indexes let you query DynamoDB by attributes other than the primary key, and how to choose between them. You practise Serverless Backend with AWS Lambda & API Gateway with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Serverless Backend with AWS Lambda & API Gateway?
No prior experience is required. Serverless Backend with AWS Lambda & API Gateway on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Querying with Secondary Indexes: GSIs and LSIs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Serverless Backend with AWS Lambda & API Gateway lesson?
Yes. Every Serverless Backend with AWS Lambda & API Gateway lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to DynamoDB
- Designing DynamoDB Tables
- Lambda & DynamoDB Integration
- Querying with Secondary Indexes: GSIs and LSIs