Search

Search papers and lessons

Columnar Storage

Level 3 · 11 min read

Columnar storage keeps each table column in its own contiguous block, so an analytical query reads only the columns it names, and similar values in one column compress well.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Databases
  • Information theory
  • ReadReady to readNot yet

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:

idcitysales
1Oslo10
2Bergen7
3Oslo4
4Oslo9
  1. 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 the city values alone means skipping past the id and sales in every row.
  2. Lay the columns out one after another. A column store writes all the id values, then all the city values, then all the sales values: 1 2 3 4, then Oslo Bergen Oslo Oslo, then 10 7 4 9. Check: each column is now one contiguous run of values, with the same type throughout.
  3. 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.
  4. Answer the question on the column store. The engine reads only the city column, four values, and finds that positions 1, 3 and 4 hold Oslo. It then reads only those positions from the sales column, which gives the same 10, 4 and 9. The id column is never touched. Check: it read 4 values from city and 3 values from sales, which is 7 values in total, not 12.
  5. Compress the city column. The city values in the column are Oslo 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.
  6. 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]
024681000.20.40.60.81columns the query needs (out of 10)fraction of the table read
  • row store: all columns
  • column store: needed columns
A row store reads the whole table whatever the query names. A column store reads a fraction equal to the share of columns needed. The marked point is a query that needs 2 of 10 columns.

The math

Step 1 in symbols: the layouts

Let a table have RR rows and CC columns, and let column jj take wjw_j bytes per value. In a row store, a row takes ∑j=1Cwj\sum_{j=1}^{C} w_j bytes, so reading the whole table costs

Brow=R∑j=1Cwj.B_{\text{row}} = R \sum_{j=1}^{C} w_j.

In a column store, each column is stored contiguously, so the cost depends only on the columns named in the query, the set QQ:

Bcol=R∑j∈Qwj.B_{\text{col}} = R \sum_{j \in Q} w_j.

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 mm rows match out of RR, the cost of the second read is m⋅wjm \cdot w_j for each extra column, not R⋅wjR \cdot w_j.

Step 3 and 4 in symbols: the example

In the walkthrough, the filter reads R=4R = 4 values from city and then m=3m = 3 values from sales, so the total is 4+3=74 + 3 = 7 values. The row store reads 4×3=124 \times 3 = 12 values.

Step 5 in symbols: run-length encoding

A column of RR values with kk runs is stored as kk pairs (value, run length). The stored size is roughly kk times the size of one pair, so the code is short when kk is much smaller than RR. Equivalently, the compression ratio is about R/kR / k when pairs and values are the same size.

Worked examples

Example
1. Tiny case: rows versus columns

Use the table from the walkthrough: R=4R = 4 rows, C=3C = 3 columns, and the query needs city and sales.

  1. Lay the rows out: 1 Oslo 10, 2 Bergen 7, 3 Oslo 4, 4 Oslo 9. Interpretation: the columns are mixed inside each row.
  2. Lay the columns out: 1 2 3 4, Oslo Bergen Oslo Oslo, 10 7 4 9. Interpretation: each column is one run of values.
  3. Count the row store's reads: all 3 columns, so 4×3=124 \times 3 = 12 values.
  4. Count the column store's reads: 4 values of city, then 3 values of sales at the matching positions, so 4+3=74 + 3 = 7 values.
  5. Compare the two: 7 values against 12. Interpretation: the column store reads fewer values because it skips id entirely.
Example
2. Standard case: a wide table, a narrow query

A table has R=1,000,000R = 1{,}000{,}000 rows and C=10C = 10 columns, each value 4 bytes, so wj=4w_j = 4 bytes. The query needs 2 columns.

  1. The row store reads all columns: Brow=1,000,000×10×4=40,000,000B_{\text{row}} = 1{,}000{,}000 \times 10 \times 4 = 40{,}000{,}000 bytes, which is 40 MB.
  2. The column store reads 2 columns: Bcol=1,000,000×2×4=8,000,000B_{\text{col}} = 1{,}000{,}000 \times 2 \times 4 = 8{,}000{,}000 bytes, which is 8 MB.
  3. Take the ratio: 40/8=540 / 8 = 5. Interpretation: the column store reads one fifth of the data, which is the share of columns needed, 2/102/10, turned upside down.
  4. Check: the same ratio holds whatever RR is, because RR cancels. Interpretation: the saving depends on the shape of the table and the query, not its size.
Example
3. Realistic: a city column with dictionary encoding

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 1,000,000×6=61{,}000{,}000 \times 6 = 6 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 ⌈log⁡250⌉=6\lceil \log_2 50 \rceil = 6 bits, using the base-2 logarithm, since 25=32<50≤64=262^5 = 32 < 50 \le 64 = 2^6.

  1. Count the codes: 1,000,000 codes at 6 bits each is 6,000,0006{,}000{,}000 bits.
  2. Convert to bytes: 6,000,000/8=750,0006{,}000{,}000 / 8 = 750{,}000 bytes, which is 0.75 MB.
  3. Add the dictionary: 50 names of 6 bytes is 300 bytes, which is negligible next to 0.75 MB.
  4. Take the ratio: 6/0.75=86 / 0.75 = 8. Interpretation: the column is 8 times smaller, and a scan now reads 8 times less data from disk.
  5. 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: ⌈log⁡220⌉=5\lceil \log_2 20 \rceil = 5 bits, since 24=16<20≤32=252^4 = 16 < 20 \le 32 = 2^5. The encoded column is 1,000,000×5/8=625,0001{,}000{,}000 \times 5 / 8 = 625{,}000 bytes, which is 0.625 MB, so the ratio is 6/0.625=9.66 / 0.625 = 9.6. 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.

Check yourself

QuestionA table has 8 columns of equal width. A query needs 2 of them. What fraction of the data does a column store read, compared with a row store?Show answer
A column store reads 2 of the 8 columns, so it reads 2/8=1/42/8 = 1/4 of the data. A row store reads all 8, so the column store reads a quarter as much. The fraction is 25 percent.
QuestionA column holds the values A, A, A, B, B, C. Write its run-length encoding and state how many pairs it uses.Show answer
The runs are A three times, B two times and C once, so the encoding is (A,3)(B,2)(C,1)(A, 3)(B, 2)(C, 1). It uses 3 pairs to represent 6 values.
QuestionWhy does a column store usually handle a sales report better than a checkout system that reads one customer's full record?Show answer
A report reads a few columns across many rows, which a column store does by reading only those columns contiguously. A checkout reads all the columns of one row, which a row store does in one place, while a column store must gather the values from each column file separately.
QuestionA company wants to speed up a daily report that scans 200 columns of a table but only needs 5 of them. Its engineers propose adding a column layout for the whole table. What should you check first?Show answer
Check the share of columns the report needs and whether other workloads read whole rows or write single records. Here 5 of 200 is 5/200=2.55/200 = 2.5 percent, so the scan could read about 40 times less data. But if the same table serves single-record lookups or frequent single-row updates, those workloads may slow down, so the design should separate the two, or keep both layouts.

Sources