Pangram verdict · v3.3
We believe this text is mainly human-written, with some AI content.
AI likelihood · overall
HumanArticle text · 1,149 words · 12 segments analyzed
Back to blogConnor TsuiPublished Nov 11, 2026 · 12 min readWhen we think about structured data, we generally think in terms of rows. Take this simple CSV file, which records some data about bike rides on a bike-sharing app in New York City: Data in this CSV file is organized into rows, where each row corresponds to a record of data. Each record here is one ride, or one "thing", with everything we know about it on one line. We tend to organize data like this almost everywhere: think of spreadsheets, JSON objects, C-style structs in an array, SQL tables, etc. This is because it matches how we think: one "thing", and all of the details of that "thing".
This way of thinking fits a lot of the software we write.
When you log in to an app, post a comment on a video, add an item to a shopping cart, send a bank transfer, or book a ride, the application will change just one record (or a few, at most).
So it makes sense that the databases behind this software, like Postgres and MySQL, "think in" (or are modeled around) rows as well: they keep each record's details together, so finding, updating, or creating a record means going to one place. But not everything we ask of a database is limited to one record. If you run the bike-share service that produced the CSV file above, you'll eventually want to learn something from all the data it collects.
This kind of work is called analytics.
For example, which neighborhood is busiest on Monday and Friday mornings?
Do people take longer rides on electric bikes than on classic ones?
Observe how no single record can answer these questions. The answers come from the dataset as a whole, so the only way to get them is to look at all of the records.1 Notice what these questions don't need, however. Finding the busiest neighborhood only needs the start_area and started_at columns, and comparing bike types only needs bike_type and duration_min. Each question may have to look at every row, but it only needs a few of the columns from each one. And a database that keeps each record's details together can't take advantage of that: to read two columns, it has to read all five. We may think in rows, but to answer questions like these quickly, we have to program in columns. Programming in columns begins with how the data is stored, but that's only the start. Once data is stored by column, the way a database runs a query changes dramatically: how much data it reads, how many values it handles at a time, how many values the CPU can process with one instruction, how well the data compresses, etc. This post will go over these changes, and by the end, you'll see just how much follows from the "simple" choice to store columns instead of rows. Row stores and column stores Let's start with what doesn't change. Whether a database stores rows or columns, the table it shows you when you do SELECT * FROM rides; is the same. You create it with the same SQL, you query it with the same SQL, and you get the same answers. We can say that the two tables are logically equivalent. Rows vs columns is a physical distinction rather than a logical one: the data represents the same thing, but how that data is actually arranged in memory and on disk is different. Databases are named after the grouping they choose. A row store keeps each record's values next to each other, just like the CSV we started with. Postgres and MySQL, which we mentioned before, are both row stores. They're built for the small reads and writes behind an application, which is called online transaction processing (OLTP).2 A column store does the opposite: it takes every record apart and keeps each column's values next to each other instead.3 DuckDB and ClickHouse are examples of column stores. They're built for analytics, which is called online analytical processing (OLAP).4 Rows and columns in memory To make this concrete, imagine that we have to hold our rides in memory ourselves. This isn't how a real database would be implemented, but it's a good approximation of how each kind arranges its data. In Rust, a row store would be a Vec of structs: A column store will have the same fields, but it will look like a struct of Vecs: Here is what these data layouts look like side by side: Both of these forms represent the same logical values, but their layout is dramatically different. Reading only the columns you need Let's do a fun exercise.5 These are the two SQL queries shown above. Which layout is better for executing each of them? For the AVG query, computing it requires only the duration_min of every ride and nothing else. In the Columns layout, values are stored together in a single Vec<u32>, so the query can scan that one array without touching the other four. For Vec<Ride> (the Rows layout), there is simply no way to read only the durations. Each duration is stored within a Ride, so the query has to read through every Ride and pass over the other four fields every time. Note that the code can "ignore" those other fields, but the hardware cannot. A CPU doesn't fetch memory one value at a time: it will fetch fixed-size blocks called cache lines (which are 64 bytes on most machines6), fetching the whole line even when the program needs only a few bytes of it. A Ride is 48 bytes7 and duration_min is only 4 of them, so with the row layout, we have to fetch all 48 bytes to read those 4, which is 12x the amount that we actually need. The CPU can't skip any of these lines either because a Ride is smaller than a cache line, so every cache line of the array has a duration somewhere in it. This diagram only shows 8 rides for simplicity (which is why the duration_min line is half empty), but the factor of 12 doesn't depend on the size of the table. With one million rides, we might be fetching 48 MB vs 4 MB.
The second query goes the other way.
SELECT * FROM rides WHERE ride_id = 1004 wants every field of a single ride, not just one.
Once the database finds the record with that ride (probably via an index8), Vec<Ride> has all of its fields next to each other, so reading all of them touches only one or two cache lines (it might overlap two cache lines).9 On the other hand, Columns has spread the same ride over five different arrays, so the query has to fetch at least five cache lines to put it back together.
For this query, the Rows layout wins.