Rows, Columns and the Separator Problem
Last timeDates, Times, and the Day It Moved
A table can be written row by row or column by column, and the choice decides how much has to be read to answer a question. Separators decide whether it can be read at all.
Rows first or columns first
A table has two dimensions and a file has one, so writing the table down means choosing an order to flatten it in. There are two sensible choices.
Row by row puts the whole first record, then the whole second, and so on. It is what a spreadsheet export looks like and what most databases do, and it suits work that wants complete records: fetch this customer, show this order, append a new row at the end.
Column by column puts every value of the first field, then every value of the second. It looks strange written out and it suits work that wants a few fields of everything: the average of this column, the count of rows where that column is above a threshold, a sum grouped by a third.
The reason the choice matters is that storage is read in large contiguous chunks, not one value at a time. Values that sit next to each other arrive together whether they were wanted or not, so the layout decides how much of what arrives is useful.
| values read to sum one column of four | values read to fetch one whole row | cheap to append one row | |
|---|---|---|---|
| row by row | 16 | 4 | 1 |
| column by column | 4 | 4 | 0 |
What a column layout buys
The saving from a column layout is the proportion of columns the query does not touch, which makes it worth putting into arithmetic.
| step | what is being worked out | column by column | row by row | the multiple | what happened |
|---|---|---|---|---|---|
| 1 | the table on disk | 100 GB in 50 columns, so 100 / 50 = 2 GB a column | 100 GB in 50 columns | the same bytes either way, so nothing is saved by the layout itself | Two gigabytes a column is the number every row below is built from. |
| 2 | a query that wants 5 of the columns | 5 x 2 = 10 GB | all 100 GB, because the 5 wanted values sit between the 45 unwanted ones | 10 times the read for the same answer | The unwanted 45 columns arrive whether or not they were asked for, because storage is read in large contiguous chunks. |
| 3 | what a budget of 10 GB of reading buys | 5 columns of every row | a tenth of the rows, all 50 columns each | 5 columns of everything against everything about a tenth | The budget is the same in both; what differs is whether it is spent on the fields the query named or on the rows it happened to reach first. |
| 4 | the same query on a table of only 3 columns | 2 x (100 / 3), so about 67 GB | all 100 GB | 1.5 times, which is close enough to nothing | A table with almost no columns to skip has almost nothing to save, which is when this choice stops being worth arguing about. |
There is a second effect that compounds the first. A column holds values of one kind, often with few distinct values and long runs of repetition, which makes it compress far better than a row where every adjacent value is a different sort of thing. The next lesson is about why that is, and the two effects together are why a column layout commonly reads a tenth of what a row layout does.
The lesson stops here
4 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents