Sparse Indexes and GSI Overloading
Two techniques make secondary indexes far more useful.
Introduction
Two techniques make secondary indexes far more useful. A sparse index contains only items that have the index key attribute, so it acts as a pre-filtered list. GSI overloading reuses one index for several entity types by storing different kinds of values in generic key attributes.
Purpose of this lesson
You will build a sparse index of open support tickets and overload GSI1 to serve both orders-by-status and products-by-category.
Understanding the topic
How they work:
- DynamoDB writes an item to a GSI only if the item has the index's partition key attribute (and sort key, if defined).
- Set the GSI attribute only on items that should appear (open tickets, flagged accounts); remove it to drop the item from the index.
- Overloaded GSI keys use prefixes (
STATUS#…,CATEGORY#…) so different entity types never collide. - Fewer indexes means fewer replicas to pay for and monitor — tables support up to 20 GSIs by default, but most well-designed tables need two or three.
Informative example
Sparse index of open tickets: only open tickets carry openSince:
import { UpdateCommand } from "@aws-sdk/lib-dynamodb";import { ddb, TABLE } from "./ddb";// GSI "OpenTickets": partition key = ticketQueue, sort key = openSinceexport async function openTicket(id: string, queue: string, now: string) {await ddb.send(new UpdateCommand({TableName: TABLE,Key: { PK: `TICKET#${id}`, SK: "TICKET" },UpdateExpression: "SET ticketQueue = :q, openSince = :now, #s = :open",ExpressionAttributeNames: { "#s": "status" },ExpressionAttributeValues: { ":q": queue, ":now": now, ":open": "OPEN" },}));}export async function closeTicket(id: string) {// Removing the GSI key attribute removes the item from the sparse index.await ddb.send(new UpdateCommand({TableName: TABLE,Key: { PK: `TICKET#${id}`, SK: "TICKET" },UpdateExpression: "SET #s = :closed REMOVE openSince",ExpressionAttributeNames: { "#s": "status" },ExpressionAttributeValues: { ":closed": "CLOSED" },}));}
Useful templates
Overloaded GSI1 key values
One index answers 'orders by status, oldest first' and 'products in a category by price'. Zero-padded prices sort correctly as strings.
Entity GSI1PK GSI1SKOrder STATUS#PLACED 2026-11-19T09:00:00Z#o1Order STATUS#SHIPPED 2026-11-19T09:30:00Z#o2Product CATEGORY#books PRICE#0000039.00#p9Product CATEGORY#books PRICE#0000012.50#p3
Best practices
- Use sparse indexes for 'needs attention' lists: open tickets, unpaid invoices, items awaiting review.
- Document which entity types write which GSI key formats.
- Pad numeric values used in string sort keys.
Common mistakes
- Setting the GSI attribute to an empty string or null-like value — empty strings are not allowed in key attributes; remove the attribute instead.
- Overloading so aggressively that one index has conflicting hot keys.
- Forgetting that removing the attribute is the only way out of a sparse index.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhat is a sparse index and why is it efficient?+
Answer
2IntermediateQuestionWhat is GSI overloading?+
Answer
Hands-on exercise
Hands-on: create the OpenTickets GSI, open five tickets in two queues, close two of them, and Query the index for each queue. Confirm closed tickets no longer appear.
Summary
Sparse indexes act as pre-filtered lists, and overloaded GSIs serve several access patterns with one index. Both reduce cost and the number of indexes to operate.