Basis Lab

Bytes, Text and File Formats

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.

FIG 1The same four rows, flattened two ways
values read to sum one column of fourvalues read to fetch one whole rowcheap to append one row
row by row1641
column by column440
A table of four rows and four columns, so sixteen values in total. Coded 1 for yes and 0 for no in the last column. The first column of the figure is the whole argument: summing one field row by row drags in every other field with it, whereas column by column reads exactly the four values wanted. The second column shows there is no free lunch, since a whole row is scattered across four places in a column layout, and the third shows the other cost, because appending a row means touching every column's run.

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.

FIG 2A table of 100 gigabytes in 50 columns, read both ways
stepwhat is being worked outcolumn by columnrow by rowthe multiplewhat happened
1the table on disk100 GB in 50 columns, so 100 / 50 = 2 GB a column100 GB in 50 columnsthe same bytes either way, so nothing is saved by the layout itselfTwo gigabytes a column is the number every row below is built from.
2a query that wants 5 of the columns5 x 2 = 10 GBall 100 GB, because the 5 wanted values sit between the 45 unwanted ones10 times the read for the same answerThe unwanted 45 columns arrive whether or not they were asked for, because storage is read in large contiguous chunks.
3what a budget of 10 GB of reading buys5 columns of every rowa tenth of the rows, all 50 columns each5 columns of everything against everything about a tenthThe 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.
4the same query on a table of only 3 columns2 x (100 / 3), so about 67 GBall 100 GB1.5 times, which is close enough to nothingA table with almost no columns to skip has almost nothing to save, which is when this choice stops being worth arguing about.
4 steps
The whole of the argument for a column layout is in the last column of the second row, and the whole of the argument against caring is in the last column of the fourth. The saving is the proportion of columns the query does not touch, so it is large on a wide table and vanishes on a narrow one, and the third row is the honest way to state it: the same 10 GB of reading buys the fields you named or the rows you did not.

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 contents

This 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

The rest of this course

  1. 01Everything Is a Run of Bytes
  2. 02Counting in Twos and Sixteensopening only
  3. 03Letters as Numbersopening only
  4. 04Whole Numbers, and Where They Stopopening only
  5. 05Numbers With Fractions In Themopening only
  6. 06Dates, Times, and the Day It Movedopening only
  7. 07Rows, Columns and the Separator Problemyou are here
  8. 08Making It Smalleropening only

Read alongside