accesspatterns.dev

Model a one-to-many with one partition

A parent and its children share a partition key, so one Query fetches them together.

Why

A playlist has many tracks. Instead of a join, both live in one item collection: the playlist's META record and its TRACK items all share the partition key PLAYLIST#chill.

So 'the playlist and everything in it' is a single Query. The relationship was resolved at write time, by where you put the items, not at read time by joining them.

Co-locate a parent and its children under one partition key, and the join disappears into a single Query.

Playlists

These rows are real and already here. Running an operation against them downloads a DynamoDB-compatible engine, about 850 KB, and executes it in this tab.

Try an example

One Query on PLAYLIST#chill returns the playlist record and every track under it.

pk(pk)
sk(sk)
name
owner
title
seconds
PLAYLIST#chill
METAS
ChillS
adaS
PLAYLIST#chill
TRACK#001S
WeightlessS
485N
PLAYLIST#chill
TRACK#002S
An EndingS
252N
PLAYLIST#chill
TRACK#003S
Avril 14thS
120N
PLAYLIST#focus
METAS
FocusS
linusS
PLAYLIST#focus
TRACK#001S
SubstrataS
360N
PLAYLIST#focus
TRACK#002S
Music for 18 MusiciansS
3360N
PLAYLIST#focus
TRACK#003S
Spiegel im SpiegelS
540N
PLAYLIST#focus
TRACK#004S
MetamorphosisS
600N
PLAYLIST#focus
TRACK#005S
GlassworksS
380N
PLAYLIST#energy
METAS
EnergyS
graceS
PLAYLIST#energy
TRACK#001S
Lose YourselfS
326N
PLAYLIST#energy
TRACK#002S
BangarangS
215N
PLAYLIST#energy
TRACK#003S
TitaniumS
245N
PLAYLIST#energy
TRACK#004S
LevelsS
340N
PLAYLIST#energy
TRACK#005S
One More TimeS
320N
PLAYLIST#sleep
METAS
SleepS
adaS
PLAYLIST#sleep
TRACK#001S
SleepS
480N
PLAYLIST#sleep
TRACK#002S
We Move LightlyS
300N
PLAYLIST#sleep
TRACK#003S
SamanS
510N
PLAYLIST#sleep
TRACK#004S
Nuvole BiancheS
360N
PLAYLIST#commute
METAS
CommuteS
samS
PLAYLIST#commute
TRACK#001S
RoygbivS
180N
PLAYLIST#commute
TRACK#002S
TeardropS
330N
PLAYLIST#commute
TRACK#003S
Midnight CityS
244N
PLAYLIST#commute
TRACK#004S
Open Eye SignalS
460N

Run an operation to see the raw engine response.

table Playlistskeys pk / skitems 26