accesspatterns.dev

Single-table design · Lesson 19 of 20

An index holds only what you project

Projection decides which attributes an index carries, trading storage against an extra read.

Next · Single-table designOne table, an application's worth of questions

Why

A secondary index is a copy of your items, and you choose how much of each item it copies. The lean index here projects only the order total; the full index projects everything. Both are keyed on order status.

Query the lean index and each row carries only its keys and total. Query the full index and every attribute is there. The lean index is cheaper to store and cheaper to write, but a read that needs the customer or address has to fetch it from the table, one GetItem per row. Project for the read you actually serve.

KEYS_ONLY and INCLUDE indexes are cheap but make reads fetch the rest; ALL avoids the fetch but roughly doubles the index cost.

Orders
Try an example

The lean index projects only total. Each row comes back with its keys and total, and nothing else.

pk(pk)
sk(sk)
gsi1pk
gsi1sk
customer
addr
total
lines

Run an operation to see the raw engine response.

table Orderskeys pk / skitems 8