Amazon DynamoDB Tutorial 0/30 lessons ~6 min read Lesson 17

    Sparse Indexes and GSI Overloading

    Two techniques make secondary indexes far more useful.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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:

    ts
    import { UpdateCommand } from "@aws-sdk/lib-dynamodb";
    import { ddb, TABLE } from "./ddb";
    // GSI "OpenTickets": partition key = ticketQueue, sort key = openSince
    export 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.

    text
    Entity GSI1PK GSI1SK
    Order STATUS#PLACED 2026-11-19T09:00:00Z#o1
    Order STATUS#SHIPPED 2026-11-19T09:30:00Z#o2
    Product CATEGORY#books PRICE#0000039.00#p9
    Product 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.

    2 questions
    1IntermediateQuestionWhat is a sparse index and why is it efficient?+

    Answer

    A GSI that only contains items having its key attribute. Because only matching items are indexed, a Query on it reads just the relevant subset — cheaper than filtering a larger index or scanning.
    2IntermediateQuestionWhat is GSI overloading?+

    Answer

    Using generic GSI key attributes whose values differ by entity type (with prefixes), so one GSI serves several access patterns instead of creating one index per pattern.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.