accesspatterns.dev

Reads and cost · Lesson 6 of 20

Read the latest first

A sorted timestamp makes 'the most recent' a direction, not a sort in your code.

Next · Reads and costRead a page, keep the bookmark

Why

Every reading from a fridge shares that fridge's partition key, and the sort key is an ISO-8601 timestamp. A fixed-width ISO timestamp sorts lexically in time order, so the newest reading is simply the last one in the partition.

That makes 'the latest' a direction. ScanIndexForward false walks the partition newest-first, and a Limit stops it early. Run all three: direction and limit are request fields, not a second query and a sort back in your own code.

Sort by a fixed-width timestamp and the latest N is a descending Query with a Limit, never a scan-then-sort.

ColdChain
Try an example

ScanIndexForward false reads the partition newest-first, and Limit 1 stops at the top row.

pk(pk)
sk(sk)
tempC
door

Run an operation to see the raw engine response.

table ColdChainkeys pk / skitems 11