Almacenamiento Columnar

Almacén columnar · Column store

Escrito por Darjus Kazakevic · revisado el

Keep each column together, so analytics reads only what it needs.

A row store, such as Postgres or MySQL, keeps all of a row's fields together on disk. That is ideal for loading or updating one order. An analytics query is different: it sums one or two columns over millions of rows, and a row store must read every column of every row to do it.

A column store keeps each column's values together instead. A query that touches 3 of 100 columns reads about 3% of the data, and values of one type side by side compress very well. ClickHouse, DuckDB, Snowflake, BigQuery and Redshift work this way, and so do Parquet and ORC files in a data lake. The price is paid on writes: one new row touches every column.

En esencia — Lay data out by column, so a scan reads only the columns a query names, compressed well and processed in batches. Pay for it with slow single-row writes, and keep the system of record in a row store.

Cuándo recurrir a él

Recurre a él cuando

  • Dashboards and reports that aggregate a few columns over millions of rows
  • Events, logs and metrics that are appended and rarely changed
  • A warehouse or data lake fed from the operational databases

Reconsidéralo cuando

  • The system of record: orders, accounts, anything updated row by row
  • High-rate lookups of one whole row by its key
  • Small data: below a few million rows, Postgres with the right index is fast enough

Conceptos clave

Row layout vs column layout
The same table, stored two ways. A row layout writes one record after another. A column layout writes all the ids, then all the regions, then all the amounts, each in its own file or block.
OLTP vs OLAP
OLTP is many small reads and writes of whole rows by key: orders, users, payments. OLAP is a few large queries that scan and aggregate some columns over many rows: reports and dashboards. Row stores suit the first, column stores the second.
Compression and encoding
A column holds one type, often with few distinct values or in sorted order. Light encodings shrink it before general compression runs, and less data on disk means less to read.
Vectorized execution
The engine processes a batch of thousands of values from one column per step, not one row at a time. Tight loops over plain arrays use CPU caches and SIMD instructions well, so a core can scan hundreds of millions of values a second.
Sort key and block statistics (Zone maps · Min/max indexes)
Data is stored sorted by a chosen key, and each block records the minimum and maximum of every column. A query filtered on the sort key skips every block whose range cannot match, without reading it.
Parquet-style files
Parquet splits a file into row groups. Inside each group, every column is a separate chunk with its own encoding and min/max statistics. Files are immutable: a change means writing a new file, never editing one.
Why point updates are expensive
One row is spread across a block per column, and those blocks are compressed and often immutable. Changing the row means rewriting the blocks, or writing a delete marker and a new version that a background merge folds in later.

Reglas de diseño

Keep transactions in a row store
The system of record belongs in an OLTP database. Copy its data into the column store for analytics, with batch loads or change data capture, and accept that the copy lags.
Write in large batches
Aim for thousands to millions of rows per insert, or files of hundreds of megabytes. Buffer small events upstream, in a queue or a staging table, and flush them together.
Pick the sort key from your filters
Sort by the columns most queries filter on, for example tenant and then time. Block statistics then skip most of the data. A sort key no query uses buys nothing.
Name only the columns you need
A column store reads exactly the columns a query names. select * on a wide table reads every column and throws the main advantage away.

Errores habituales

  • Inserting one row at a time, then watching merges fall behind until the store rejects writes for having too many parts.
  • Running select * on a 200-column table and concluding that column stores are slow.
  • Choosing a sort key that no query filters on, so every query reads every block.
  • Writing thousands of tiny Parquet files to a lake, so queries spend more time opening files than reading them.