Querying & Sorting
You add a filter to a Scan because it feels like SQL WHERE, then pay to read
every item in the table while the console shows twenty matching rows. That pattern
is the most common DynamoDB cost surprise on production bills.
Reads in DynamoDB are keyed. Query walks one item collection in sort-key order;
Scan touches the whole table. Filter expressions discard rows after capacity is
charged. Pagination must carry LastEvaluatedKey; sorting happens via the sort
key of the index you chose, not an arbitrary column in application memory.
What you can do after
- Choose
QueryvsScanfor a feature and defend the choice with partition scope. - Page through large result sets without dropping or duplicating items.
- Apply filters at the key condition when possible and know what a post-read filter still costs.
- Encode sort order in the sort key — descending reads, mutable keys, zero-padding for numeric order.
Reading order
- Query vs Scan — the billing decision every other read guide assumes you understand.
- Pagination —
LastEvaluatedKey, page loops, and whyLimitis not "rows returned." - Filtering strategies — key conditions vs filter expressions; push selectivity as far left as the keys allow.
- Why Scan is slow and expensive — table
growth vs partition growth; when a one-off
Scanis acceptable. - Parallel scans — segmented
Scanfor bulk export; the throughput trade-off. - Sort key strategies — design the sort key so order is on disk, not in your app.
- Query descending order —
ScanIndexForwardand newest-first reads. - Mutable sort keys — when changing the sort key is a delete+put and what that costs on GSIs.
- Zero-padding sort — fixed-width numeric
sort keys so
ORDER#00042sorts beforeORDER#00043.
A Query on PK = TENANT#acme with SK begins_with ORDER# reads only that tenant's
orders. A Scan with filter status = OPEN on a ten-million-item table still
rounds up capacity for millions of reads. At on-demand pricing in us-east-1, a
single 4 KB read unit costs about $0.000000125 — trivial per call, brutal at table scale.
The pricing calculator prices your actual
read rate and item size.
Parallel scans split a Scan across workers for bulk export jobs. They multiply
read throughput and cost proportionally — useful for one-time migrations, dangerous
as a cron job on a large table. Mutable sort keys force delete-and-put when you change
the sort position of an item, which doubles writes on any GSI that indexes that key.
Zero-padding ORDER#00042 vs ORDER#42 fixes lexicographic order when your sort key
is a string carrying numeric IDs.
In DynoTable
The free query builder composes a full Query or
Scan request with pagination loop code in eight export targets. In the app, the
visual filter builder picks an index, shows a derived Query vs Scan badge, and
flags scan-filters before you run.
Download DynoTable and execute these reads against a live table. The
query cost status bar gives a pre-run hint (~N items · ~R RCU when the plan is
known) on Workbench and PartiQL tabs so surprise bills surface before you commit.