Introduction to Data Partitioning 00:00:01 When one directory fills its machine A makerspace lists every member in one directory. Each lookup carries a membership number. Can the team divide the records without making reception search every new machine? 12 rows in one database. Simple, and good enough for a long time. 24 rows now. In real life the disk, the memory for indexes or the processor runs out. Most questions are about one user, so user_id becomes the key. Each row goes to the shard given by user_id % 3. The 24 rows give 8 per shard. Each shard serves about a third of the data and the queries, as long as users are about equally active. Choose a repeatable ownership rule first. With the directory split horizontally, each membership number leads reception to one machine holding the entire row. 00:00:56 A row cut and a column cut Draw a rectangle around four profiles and their four fields. One proposed cut separates groups of people. The other moves large fields away from contact details. Four rows, four columns. Cut between rows. Users 1 and 2 on one shard, 3 and 4 on another. Both have all columns. Cut between columns. Name and email stay together, the large columns move away. A full profile now needs two reads, joined by the user id. A large portrait can live apart from a frequently read email address. A screen that needs both pieces now pays for bringing them together by membership number. 00:01:43 Give three bad keys a hearing Before choosing a partition key, ask how many distinct values exist, whether traffic clusters on one value, and which key appears in the most frequent request. Only 5 values. There can never be more than 5 useful parts. About 200 values, and one country may hold most users. Plenty of values, but with ranges every new row lands in the newest range. Millions of values, no giant, and the main query is about one user. Count requests as well as records. Millions of unique member numbers help here because popular lookups actually supply that number, and the largest member is still manageable. 00:02:26 Follow member seven Reception receives a lookup for member seven. Three shards carry labels zero through two. Predict the destination before the router evaluates the remainder. The request reaches the router with the key. 7 mod 3 is 1. Shards 0 and 2 never hear about it. Counting all users has no user_id, so every shard must work. One member lookup can be targeted. A count covering every member lacks that routing key, so the coordinator asks all three shards and combines their answers. 00:03:01 Retire a month's records Move to an audit log, where people ask about a date interval and old months expire together. Partitioning can help even when every partition stays inside one database. Each month is its own table inside one database. January ends before March 10, and so does February. Only March can hold the answer. In April the January table can be dropped at once, without deleting row by row. A March lookup can skip January under these bounds. Dropping an expired partition is an administrative operation, so check retention and dependent records before doing it.