Store data column by column, so analytics reads only what it needs and compresses well.
A columnar database stores data column by column instead of row by row. All the values of one column sit together on disk.
A normal row store keeps each record together: id, name, country, plan, age and spend, then the next record. That is perfect when you want one customer's full details. The database finds the row and everything is right there.
An analyst asks different questions: "what is the average spend of customers over 40?" The table may have 200 columns and a billion rows, but the question uses only two columns. A row store must still read every row in full, wasting most of what it reads.
A column store keeps all the spend values together, all the age values together, and so on. The query reads two columns and ignores the other 198. On top of that, a column holds one kind of value, such as many repeats of "US" and "UK", so it squeezes into much less space.
| Trick | How it works | Helps with |
|---|---|---|
| Column pruning | Read only the columns the query names | Less data read from disk |
| Run-length encoding | Store a value and how many times it repeats in a row, like US x5 | Sorted or low-variety columns |
| Dictionary encoding | Replace long repeated values (like country names) with small numbers and keep a lookup table | Text columns with few different values |
| Delta encoding | Store the difference from the previous value, like +1, +1, +2 for timestamps | Slowly changing numbers and times |
| Vectorised execution | Process thousands of values per CPU step instead of one at a time | Sums, counts, filters |
| Min/max per block | Remember the smallest and largest value of each block, and skip blocks that cannot match the filter | Range filters like date between |
| Sort key / partitioning | Keep rows in an order that suits common filters and split data by something like date | Skipping data, and better compression |
before: free free free free pro pro team team team after: free x4 | pro x2 | team x3 (3 runs instead of 9 values) in insertion order: free pro free team pro free team pro team after: 9 runs of 1 (no saving at all)
Adding one row means adding one value to every column area, which is slow if done one row at a time. So columnar engines collect new rows in a small write buffer (often row-shaped, in memory), then write them out to column storage in large batches, and merge them in the background. That is why they prefer big bulk loads to many tiny inserts.
| id | name | country | plan | age | spend |
|---|---|---|---|---|---|
| 1 | Ana | US | free | 31 | 0 |
| 2 | Ben | UK | pro | 45 | 90 |
| 3 | Chen | US | free | 28 | 0 |
| 4 | Dia | IN | team | 39 | 240 |
| 5 | Eli | UK | pro | 52 | 90 |
| 6 | Fay | US | free | 24 | 0 |
| 7 | Gus | IN | team | 47 | 240 |
| 8 | Hana | US | pro | 36 | 90 |
Cyan = read and useful. Amber = read but not needed. Dark = not touched. Each box groups the cells that sit side by side on disk.
A tiny teaching model with 8 rows and 6 columns. Real engines read in blocks of thousands of values, keep per-block min and max values to skip data, and combine run-length encoding with other tricks such as dictionaries.
| Term | Meaning in easy words |
|---|---|
| Columnar storage | Storing each column's values together instead of storing whole rows together |
| Row store | The classic layout: one whole record after another |
| OLAP | Analytic workload: few queries that read a lot of data |
| OLTP | Transaction workload: many small reads and writes of single records |
| Column pruning | Reading only the columns a query uses |
| Run-length encoding (RLE) | Replacing a repeated value with the value and a count |
| Dictionary encoding | Replacing values with small codes plus a lookup table |
| Vectorised execution | Processing a batch of values at once |
| Zone map (min/max) | The smallest and largest value of a block, used to skip blocks |
| Sort key | The order in which rows are kept, which affects speed and compression |
| Parquet / ORC | Open file formats that store tables by column |
| Choice | We gain | We lose |
|---|---|---|
| Column layout | Fast scans and small storage | Slow whole-row reads and writes |
| Row layout | Fast single-record work | Wasted reads for analytics |
| Sorting by a column | Better compression and skipping | Other filters benefit less |
| Bigger write batches | Efficient loading | New data appears a little later |
| Wide denormalised tables | No big joins at query time | Repeated data and larger load jobs |
| Strong compression | Less I/O and disk | Some CPU time to decompress |