accesspatterns.dev

Modelling relationships · Lesson 13 of 20

One key, every level

A path in the sort key lets one begins_with read any level of a hierarchy.

Next · Modelling relationshipsA different key for a different question

Why

These weather stations live in one partition, with a sort key that encodes a path: country, then state, then city, joined by a delimiter. Because the partition is stored sorted, every station in a country sits together, and within it every station in a state sits together.

So one begins_with reads any level of the tree. A US# prefix gives a country, US#CA# narrows to a state, and the full path is a single city. No FilterExpression appears anywhere: the hierarchy is in the key, and the key narrows the read before it happens.

Encode a path into the sort key and one begins_with reads any level of the tree, in order, for free.

Geo
Try an example

begins_with(sk, 'US#') returns every US station and nothing else. A country-level prefix.

pk(pk)
sk(sk)
station
tempC

Run an operation to see the raw engine response.

table Geokeys pk / skitems 12