TL;DR
A table is normally thought of as rows, but a database can store it by columns instead. Columnar storage writes all the values of one column next to each other, then the next column, and so on. A query that asks about two columns out of twenty then reads about a tenth of the data, and values that repeat within a column compress much better than mixed rows do.
Builds on: Relational Model, Lossless Compression · Leads to: the Dremel paper and the analytical systems built on column layouts
Where it's used
Analytics warehouses. A company's sales table may have 100 columns and billions of rows. A report such as "total revenue by country for last month" touches three or four of those columns. A column store reads just those, which is why many warehouse systems use it. The report itself is usually written in SQL.
Interactive analysis of logs. Google's Dremel system ran ad-hoc queries over web-scale datasets, and the paper that describes it stored nested records column by column so that a query could scan only the fields it needed. The system was built so that people could ask questions over trillions of records and get answers in seconds.
Open data files. Apache Parquet is a file format that stores tables by column, with compression inside each column. Many data tools read and write it, so a dataset written by one program can be scanned by another without loading whole rows.
How it works
Take a tiny table with four rows and three columns, id, city and sales. The question is: what is the total sales for rows where city is Oslo?
The table is:
| id | city | sales |
|---|---|---|
| 1 | Oslo | 10 |
| 2 | Bergen | 7 |
| 3 | Oslo | 4 |
| 4 | Oslo | 9 |
- Lay the rows out one after another. A row store writes the whole first row, then the whole second row, and so on:
1 Oslo 10,2 Bergen 7,3 Oslo 4,4 Oslo 9. Check: reading thecityvalues alone means skipping past theidandsalesin every row. - Lay the columns out one after another. A column store writes all the
idvalues, then all thecityvalues, then all thesalesvalues:1 2 3 4, thenOslo Bergen Oslo Oslo, then10 7 4 9. Check: each column is now one contiguous run of values, with the same type throughout. - Answer the question on the row store. The engine reads all four rows in full, because the three columns are interleaved, and keeps the sales where the city is Oslo, which gives 10, 4 and 9. Check: it read 12 values to use 6 of them.
- Answer the question on the column store. The engine reads only the
citycolumn, four values, and finds that positions 1, 3 and 4 hold Oslo. It then reads only those positions from thesalescolumn, which gives the same 10, 4 and 9. Theidcolumn is never touched. Check: it read 4 values fromcityand 3 values fromsales, which is 7 values in total, not 12. - Compress the city column. The
cityvalues in the column areOslo Bergen Oslo Oslo. Run-length encoding, a scheme different from Huffman coding, stores the column as a list of runs,(Oslo, 1) (Bergen, 1) (Oslo, 2), which can be shorter when values repeat next to each other. Check: the 4 values become 3 pairs, each holding a value and a count. The saving is small here, but on a column with long runs of equal values it is much larger. - Read the rule. The cost of a query is the number of values in the columns it names, not the number of columns in the table. A join names the columns of both tables. Check: a query that names 2 columns of 20 reads about 2 of 20 columns' worth of data.
flowchart LR Q[Query names city and sales] --> R[Row store reads every full row] Q --> C[Column store reads city and sales only] C --> P[Positions where city is Oslo] P --> S[Sum matching sales values]
- row store: all columns
- column store: needed columns
The math
Step 1 in symbols: the layouts
Let a table have rows and columns, and let column take bytes per value. In a row store, a row takes bytes, so reading the whole table costs
In a column store, each column is stored contiguously, so the cost depends only on the columns named in the query, the set :
Step 2 in symbols: the positions
A column store can answer a filter by reading one column for the positions that match, then reading other columns only at those positions. If rows match out of , the cost of the second read is for each extra column, not .
Step 3 and 4 in symbols: the example
In the walkthrough, the filter reads values from city and then values from sales, so the total is values. The row store reads values.
Step 5 in symbols: run-length encoding
A column of values with runs is stored as pairs (value, run length). The stored size is roughly times the size of one pair, so the code is short when is much smaller than . Equivalently, the compression ratio is about when pairs and values are the same size.
Worked examples
Use the table from the walkthrough: rows, columns, and the query needs city and sales.
- Lay the rows out:
1 Oslo 10,2 Bergen 7,3 Oslo 4,4 Oslo 9. Interpretation: the columns are mixed inside each row. - Lay the columns out:
1 2 3 4,Oslo Bergen Oslo Oslo,10 7 4 9. Interpretation: each column is one run of values. - Count the row store's reads: all 3 columns, so values.
- Count the column store's reads: 4 values of
city, then 3 values ofsalesat the matching positions, so values. - Compare the two: 7 values against 12. Interpretation: the column store reads fewer values because it skips
identirely.
A table has rows and columns, each value 4 bytes, so bytes. The query needs 2 columns.
- The row store reads all columns: bytes, which is 40 MB.
- The column store reads 2 columns: bytes, which is 8 MB.
- Take the ratio: . Interpretation: the column store reads one fifth of the data, which is the share of columns needed, , turned upside down.
- Check: the same ratio holds whatever is, because cancels. Interpretation: the saving depends on the shape of the table and the query, not its size.
A log table has 1,000,000 rows, and its city column has 50 distinct names. An average name takes 6 bytes, so the uncompressed column is MB. A dictionary encoding stores each distinct name once in a small table and replaces each value with a code. With 50 distinct values, a code needs bits, using the base-2 logarithm, since .
- Count the codes: 1,000,000 codes at 6 bits each is bits.
- Convert to bytes: bytes, which is 0.75 MB.
- Add the dictionary: 50 names of 6 bytes is 300 bytes, which is negligible next to 0.75 MB.
- Take the ratio: . Interpretation: the column is 8 times smaller, and a scan now reads 8 times less data from disk.
- Try it: suppose the same column has 20 distinct names instead of 50. How many bits does each code need, and what is the ratio to the 6 MB uncompressed column? Answer: bits, since . The encoded column is bytes, which is 0.625 MB, so the ratio is . Fewer distinct values give a higher ratio.
Common mistakes and tradeoffs
It is tempting to think that a column store is always faster. In fact it wins when a query reads a few columns of a wide table, and it can lose when a query needs whole rows, as in fetching one customer's full record. Reassembling a row from many columns means many reads at different places on disk, and each read may miss the fast memory that holds recent data.
It is tempting to think that writing to a column store costs the same as writing to a row store. In fact inserting one new row touches every column file, and many systems batch new rows and write them in large blocks, so single-row updates are slow. That is why column stores are chosen for reporting, not for a busy checkout system.
It is tempting to think that compression is free. In fact the data must be decoded before a computation uses it, which costs processor time. Engines often compute directly on the encoded values, such as comparing dictionary codes instead of strings, to recover that time.
Use columnar storage when queries scan large tables and touch few columns, and when the data is mostly appended. Avoid it when the workload is mainly single-row reads and writes, where a row store or a B-tree index is simpler. Alternatives include row stores with indexes, and hybrid layouts that keep recent rows in rows and older data in columns.
History
Early database systems stored rows, because most work at the time updated one record at a time and disks were slow to seek. In the mid-2000s, research systems such as C-Store showed that for analytical work a column layout, with compression and sorted columns, could be much faster. The Google Dremel paper, published at the VLDB conference in 2010, showed that a column layout also works for nested records at very large scale, and that interactive queries over trillions of rows were possible. Open formats such as Apache Parquet then spread the idea to many tools, so the same file layout could be read by different systems.
Recap
- A column store writes each column contiguously, so a query reads only the columns it names.
- The cost of a scan is the share of columns needed, which can be a small fraction of the table.
- Repeated values within a column compress well, with run-length and dictionary encodings.
- The cost is slower writes and single-row reads, and some processing to decode.
- Read next: the Dremel paper for nested data in columns, and Database Index for the other common way to avoid reading whole tables.