Module 04 · Lesson 1 of 10Free
Row and columnar storage: why layout matters
- Level
- Beginner
- Reading time
- 13 min
When you finish this page you can
- Describe how a row layout and a columnar layout place the same table’s values on disk.
- Estimate how many bytes a report and a single-order lookup read in each layout.
- Explain why the store’s database keeps rows and the analytical copy keeps columns, and when that choice would change.
- Recognise row groups as the layout real columnar tables and files use.
Builds on 3 lessons
Start with Module 01, lesson 513 minChapters
Show transcript
0:00 Monday evening. A customer in Pune taps Pay on Kartly, an online store in India. Kartly saves one order in its database. That order is about 200 bytes. And it happens about 60,000 times a day. Every night, a copy of the orders goes into a second database. Daniel works in finance. He reads revenue per day from that copy. His report reads all 20 million orders. And it reads far fewer bytes there than it would in the store’s own database. So a developer asks Meera, the data engineer: why not run the store on that second database too? Here is the orders table. It has 10 columns: order id, customer, time, status, amount, and five more. Daniel’s report needs three of them. The time, the status and the amount. So here’s the question. Why does Kartly keep the same orders as rows in the store’s database, and as columns in the copy? And what does each layout make cheap? A layout is simply how the values are placed on disk.
0:55 The store’s database is PostgreSQL. It saves a table in pages. A page is a block of 8 kilobytes, and it is the smallest piece the database ever reads. Each page holds whole orders, with all their values side by side. That is a row layout: each record is kept together. About 35 orders fit on one page. Across all of Kartly’s orders, the table adds up to roughly 4.7 gigabytes of pages. This layout is exactly right for the store. The order page asks for one order by its number. The database finds the right page, and one read brings back every value. Checkout writes a new order into one page. A later status change touches that same place again. But a report pays for this. Daniel needs about 24 bytes of each order’s 200. The rest sits on the same pages, so it gets read too. The report reads every column of every order to use a small part.
1:48 A columnar layout turns the table the other way round. All the order ids sit together. Then all the times. Then all the statuses, and so on, one run for each column. DuckDB, the copy Kartly reports from, stores data this way. Now run Daniel’s report. It reads only the runs it names. That is about 480 megabytes for all the orders. The other columns are never touched. Compare 4.7 gigabytes on pages. That is about a tenth of the bytes. Two more things help. A run holds one kind of value, packed tight, so the engine can add them up in one fast loop. And similar values side by side shrink well. Status has only four possible values, repeated 20 million times.
2:29 Now the developer’s idea. Could the store run on a columnar database? The order page needs every column of one order. In a row layout, that is one page. In a columnar layout, the values sit in ten different runs. The engine has to find and unpack each one. Writing is worse. Checkout adds one value to the end of each run. A packed run can’t simply take a new value at its end. It has to be packed again, or the value waits aside until later. And a status change rewrites a value inside a packed run. Columnar databases know this. DuckDB’s own guide says to avoid many single-row inserts, and to put as much data as possible into each statement. Each one-row insert is saved to disk on its own. So a column store wants a few big writes. That is called a bulk load. It suits the nightly copy, which loads a whole day at once. It does not suit checkout. The lesson is a trade. A layout that makes one kind of work cheap makes another kind pay. Rows make one-record work cheap. Columns make work cheap that touches many records but few columns.
3:28 A real columnar table is not one long run per column from the first order to the last. To find one order, you would jump across the whole table ten times. So the table is cut into row groups. A row group is a block of rows, stored column by column inside. In DuckDB, a row group holds 122,880 rows. For Kartly, that makes about 163 row groups. All the values of one order sit in the same group, at the same spot in each column. So rebuilding one order searches one group, not the whole table. A report still reads only the columns it names, from every group. This mix has a name: PAX. It comes from a 2001 paper. Parquet files use the same idea, and a later lesson opens one up. Row groups soften the cost of reading one order. They don’t remove it.
4:14 So where should Kartly keep its orders? Option one: rows only. Reports run on the store’s own data. One copy, always fresh. But every report reads all 4.7 gigabytes. Option two: columns only. Reports read about a tenth of the bytes. But every checkout writes to many places, all day. Option three: both. Rows in PostgreSQL for the store. A nightly columnar copy in DuckDB for reports. Each job gets the layout that makes its common question cheap. The cost is a second copy and a nightly job, so reports can be a day old. And a nightly bulk load is exactly the write a column store likes. When would the choice change? With small data. A month of orders is about 1.8 million rows, so an occasional report on the store’s own data is fine. Or when numbers must be fresh. A dashboard under a minute behind can’t wait for a nightly copy. Then Kartly streams changes into a columnar engine built for constant small writes.
5:06 Let’s recap. One: Kartly keeps rows for the store and columns for reports, because each layout makes a different question cheap. Two: a row layout keeps each order whole on a page, so one order is one read. Three: a columnar layout keeps each column together, so a report reads only what it names, about a tenth of the bytes. Four: columns make one order expensive, so load them in bulk. Five: real columnar tables use row groups, so one row’s values stay together. Not every file Meera loads is columnar. Partners send CSV and JSON, which store rows as plain text. Next, we’ll see what that costs. See you in CSV and JSON.
One order written, 20 million read
At 20:14 on Monday 9 November, a customer in Pune taps Pay. , an online store in India, writes one order into its PostgreSQL database: about 200 bytes. It does that about 60,000 times a day.
At 01:00 the next night, a job copies the day’s orders and click events from a into . Meera, Kartly’s data engineer, runs it. Daniel, who leads finance reporting, reads revenue per day from that copy at 07:00. His query reads all of Kartly’s orders, about 20 million.
A developer on the app team asks Meera a fair question. Daniel’s query reads a fraction of the bytes in DuckDB that it would on the replica. So why not run the store on DuckDB too?
This lesson answers it: why does Kartly keep the same orders as rows in the store’s database and as columns in the analytical copy, and what does each layout make cheap?
The store’s orders table has 10 columns: order_id, customer_id, ordered_at, status, amount_inr, payment_method, city, pincode, coupon_code and updated_at. Daniel’s report needs three of them.
Daniel’s report needs ordered_at, status and amount_inr: about 24 of each order’s 200 bytes. If he ran it on the PostgreSQL replica, what share of the table’s bytes would PostgreSQL read?Show answer
Row layout: one order’s values side by side · 200 bytes
- ordered_at, status, amount_inr24 byteswhat Daniel’s report needs
- the other seven columns176 bytesread anyway: they sit on the same page
A scan reads the whole page, so every byte comes along.
Columnar layout: each value in its column’s run · 200 bytes
- ordered_at8 bytesread
- status8 bytesread
- amount_inr8 bytesread
- the other seven columns176 bytesnever touched: stored in their own runs
The report reads three runs; the other seven stay on disk.
Rows sit whole on 8 KB pages
PostgreSQL stores every table as an array of , usually 8 kB each 1. A page starts with a 24-byte header 1. Then comes a 4-byte pointer for each row on it 1. Each row carries its own header, 23 bytes on most machines, and then its values 1.
So an order’s 10 values sit next to each other on one page. This is a row layout, also called : each record is kept together. Here is what it means for Kartly’s orders:
Bytes per row on the page
200 bytes of values + 23-byte header (padded to 24) + 4-byte pointer ≈ 228 bytes
One order, as PostgreSQL stores it.
Rows per page
(8,192 − 24) bytes ÷ 228 bytes ≈ 35 orders
A page holds whole orders, about 35 of them.
Pages for every order
20,000,000 ÷ 35 ≈ 571,000 pages × 8 KB ≈ 4.7 GB
What Daniel’s report would read on the replica.
An estimate for this scenario. It ignores free space left by updates and the indexes.
That layout is exactly right for the store. The order page asks for one order by id, a . An index finds the page, and one 8 KB read returns all 10 values. Checkout writes one new order into one page with free space. Changing the status later touches that one place again.
A report pays for this. It needs 24 bytes of each row, but the other 176 bytes share the same pages, so they are read too.
Columns sit together, one run per column
A columnar layout stores the same table the other way round. All the order_id values sit together, then all the ordered_at values, and so on for each column. That is , the layout DuckDB uses. Columnar and wide-column databases compared showed the idea; here is what it does for Daniel’s query.
Bytes per order for the report
ordered_at 8 + amount_inr 8 + status about 8 ≈ 24 bytes
Only the three columns the query names.
Bytes for every order
20,000,000 × 24 bytes ≈ 480 MB
The other seven columns are never read.
Compared with the row layout
480 MB ÷ 4.7 GB ≈ 10%
Before compression, which helps the columns more.
An estimate. Byte widths are typical for these types; status is text of about 8 bytes before compression.
Reading fewer bytes is the gain you have met before. Writing fast analytical queries priced queries by the columns they name, and Reading and writing CSV, JSON and Parquet in Python read two columns out of a file. This lesson is about why it works: it is the layout, not the engine’s cleverness.
The layout brings two more gains. First, a run of one column holds values of one type, packed together. The engine can sum them in a compiled loop over a block of values, the from The Python data toolkit. Second, similar values side by side shrink well. status has four possible values, repeated 20 million times. Compression and encoding shows how files exploit that.
Research on column stores found that compression and working on blocks of values explain much of their advantage, not only the columns skipped 4.
Columns make one order expensive
Now the app developer’s idea. The store asks a different kind of question, so run it through the columnar layout.
Row layout
Columnar layout
- Reading one order. The order page needs all 10 columns. Each sits in its own run, inside a compressed block. The engine finds 10 blocks and decodes each to pull out one value.
- Writing one order. Checkout adds 10 values, one to the end of each run. A compressed block can’t take a value at its end for free; it is encoded again or kept aside until later.
- Changing one order. A status update rewrites a value inside a packed, compressed run, instead of one row on one page.
Columnar engines know this. DuckDB’s documentation says to avoid many single-row INSERT statements and to put as much data as possible into each statement 2. In auto-commit mode, each statement is its own transaction with its own sync to disk 2.
So a column store wants a few large writes. That suits Kartly’s nightly job: it loads each day as one , millions of rows in one statement. It does not suit checkout, which writes one order at a time, all day.
The general lesson is in Storage engine tradeoffs: a layout that makes one kind of work cheap makes another pay. Rows make one-record work cheap. Columns make many-record, few-column work cheap.
Row groups keep a row close to itself
A real columnar table isn’t one run per column from the first row to the last. Finding order 48213907’s 10 values would then mean jumping across the whole table 10 times.
Instead, the table is cut into : blocks of rows, each stored column by column. DuckDB’s storage format works this way: “row groups, i.e., horizontal partitions of the data” 3. A row group holds 122,880 rows by default, as the zone maps in Writing fast analytical queries showed.
Daniel’s report
| row group | report’s three | other seven |
|---|---|---|
| first | read | skipped |
| second | read | skipped |
| … | … | … |
| last | read | skipped |
ordered_at, status and amount_inr, in each of the 163 groups.
Order page, order 48213907
| row group | report’s three | other seven |
|---|---|---|
| first | skipped | skipped |
| … | … | … |
| its group | read | read |
| … | … | … |
| last | skipped | skipped |
All ten chunks of one group of 122,880 rows, the order at the same position in each.
For Kartly, that is 20,000,000 ÷ 122,880 ≈ 163 row groups. Every value of one order sits in the same row group, at the same position in each column chunk. So rebuilding one row searches one group, not the whole table. A report still reads only the chunks it names.
This hybrid has a name: , from a 2001 paper that kept a page’s rows together but stored them column by column inside it 5. files use the same idea, with row groups and column chunks. Parquet: columnar files for analytics opens one up.
Row groups soften the cost of one-order reads. They don’t remove it: a single new order still touches 10 chunks.
Rows for the store, columns for the reports
Where should Kartly keep the orders that the store and the reports both need?
Rows only: run reports on the PostgreSQL replica.
One copy, always fresh. But every report reads all 4.7 GB of pages, and a long scan on the replica competes with the replica’s other job: being a ready copy of the store.
Columns only: run the store on a columnar engine.
Reports read about a tenth of the bytes. But every checkout writes 10 places, every order page reads 10, and every status change rewrites part of a compressed run, all day.
Both: rows in PostgreSQL for the store, a nightly columnar copy in DuckDB for reports.
Each workload gets the layout that makes its common query cheap. The cost is a second copy and a nightly job, so reports are up to a day old.
Why this choice
The store is : one order at a time, read and written all day. The reports are : millions of orders, three columns. Operational vs analytical data gave the analytical copy other reasons to exist, such as keeping history. Layout is one more. A nightly bulk load is also exactly the write a column store handles well.
When the choice changes.
- Small data. A month of orders is about 1.8 million rows. An occasional query over that on the replica is fine, and a second copy isn’t worth building for it alone.
- Fresh numbers. The festival dashboard at stage 3 must be under a minute behind. A nightly copy can’t do that, so Kartly streams changes into a columnar engine built for constant small inserts. Real-time OLAP covers how those engines batch writes internally.
Try it yourself
DuckDB will show you its own layout. Install it (Python 3.12, as in this course):
pip install duckdb==2.0.0Save this as layout.py and run python layout.py:
import duckdb
# A file, not memory: DuckDB compresses only databases stored on disk.
con = duckdb.connect("layout.duckdb")
con.execute("""
CREATE OR REPLACE TABLE orders AS
SELECT i AS order_id,
i % 200000 AS customer_id,
['placed', 'shipped', 'delivered', 'cancelled'][1 + i % 4] AS status,
CAST(100 + i % 5000 AS DECIMAL(12, 2)) AS amount_inr
FROM range(1000000) AS t(i)
""")
con.execute("CHECKPOINT") # write everything to the file
rows = con.execute("""
SELECT column_name,
count(DISTINCT row_group_id) AS row_groups,
string_agg(DISTINCT compression, ', ' ORDER BY compression) AS compression
FROM pragma_storage_info('orders')
WHERE segment_type <> 'VALIDITY' -- skip the null-mask segments
GROUP BY column_name
ORDER BY min(column_id)
""").fetchall()
for name, groups, compression in rows:
print(f"{name:12} {groups} row groups {compression}")The output should look like this (compression names vary by version):
order_id 9 row groups BitPacking
customer_id 9 row groups BitPacking
status 9 row groups DICT_FSST
amount_inr 9 row groups BitPackingLook for two things. A million rows make 9 row groups: 1,000,000 ÷ 122,880 ≈ 8.1, so 8 full groups and one partial. And each column lists its own compression, because DuckDB chooses an encoding per column. status, with four values repeated, should get a different one from order_id.
Where this shows up
- Columnar execution and massively parallel processing builds a cloud warehouse’s engine on this layout.
- Wide tables: when one big table works relies on it: in columns, a table with 80 columns costs a query only the ones it names.
- Case: Zerodha’s back office on ClickHouse moves reporting from a row store to a columnar engine.
Not every file Meera loads is columnar, though. Partners send CSV and JSON, row formats written as text, and the next lesson prices what that costs at scale.
Common mistakes
Every extra column is another run to read and decode, and the engine must stitch the rows back together. Name the columns you need.
A loop of single-row inserts makes each row a transaction and a sync to disk. Load each night in one bulk statement.
It is faster for scans of a few columns over many rows. For one record at a time, a row layout does less work.
Check your understanding
The finance team adds a 40-column table of payment details to the DuckDB copy. Daniel’s daily revenue query doesn’t use it. Does it read more bytes now?Show answer
The app team’s order page asks for one order’s 10 columns. Which layout reads fewer places, and why?Show answer
Meera’s new script inserts each of a night’s 10 million clicks with its own INSERT, in auto-commit mode. What goes wrong?Show answer
Why does a columnar table keep its rows in row groups instead of one long run per column?Show answer
Summary
- Kartly keeps orders as rows for the store and as columns for reports, because each layout makes a different kind of query cheap.
- A row layout stores each record whole on a page, so one order is one read or one write, but a scan reads every column.
- A columnar layout stores each column together, so a report reads only the columns it names, about a tenth of the bytes for Daniel’s query, and those columns process and compress well.
- Columns make single-record work expensive: one order is 10 places to read or write, so column stores want bulk loads, not row-by-row inserts.
- Real columnar tables and files use row groups, stored column by column inside each group, so one row’s values stay together.
