The same query: 30 minutes vs 200 ms — column stores read only the columns touched
The same SELECT against the same row count runs ~9000x faster on a columnar engine because the row store reads the whole table while the column store reads only the columns the query touches.
Scene 01
The same query: 30 minutes vs 200 ms
- Watch
- Try it
- Predict
- Capture
Same query, two engines. The row store pulls 360 GB off disk and spins to 30 minutes. The column store pulls 28 GB and finishes at 200 ms. Same hardware, same row count — only the on-disk shape differs.
Highlighted lines are the ones running in the diagram right now.
def scan(query): # Postgres-shapedbytes_read = 0for page in heap.pages(): # 8 KB pages, row-majorfor row in page.rows(): # all N cols interleavedbytes_read += row.width # = sum(width(c) for c in N)tuple = decode(row) # 30 fields materializedproject = [tuple[c] for c in query.columns]emit(project) # K-of-N kept, N-K discardedreturn bytes_read # ~= table_size_bytes
def scan(query): # ClickHouse-shapedbytes_read = 0files = [open(f'{c}.bin') for c in query.columns] # K files# the other (N - K) column files are never openedfor granule in zipGranules(files): # vector of K colsbytes_read += sum(len(b) for b in granule)emit(granule) # already projectedreturn bytes_read # ~= (K / N) * table_size
def compare(query, table):N = table.totalColumns # 30 or 100K = len(query.columns) # 3, 30, or 1row_bytes = table.size_bytes # always full tablecol_bytes = table.size_bytes * (K / N)ratio = row_bytes / col_bytes # = N / Kreturn (row_bytes, col_bytes, ratio)
Where this sits in Build a columnar OLAP store (ClickHouse / Druid style)
Scene 01 of 13, in the Why columnar? act — The 30-min Postgres query vs the 200ms ClickHouse query — what's on disk?. Same SELECT, same rows. ClickHouse runs 9000× faster than Postgres because the column store reads only the columns the query touches.
Up next. If both engines hold the same 1.2 billion rows, the only thing that can explain a 9000x gap is what each engine actually pulls off disk — so we need to look at the physical layout under the rows.
All 13 scenes in Build a columnar OLAP store (ClickHouse / Druid style) · Every curriculum