Part 9 of 10
Writing to column-oriented storage
Warehouse reads are big scans and totals, and columns, compression, and sorting all speed them up. Writes are mostly bulk loads, often from ETL jobs. Inserting one row into the middle of a sorted, compressed column would mean rewriting the column from that point on. Writing in bulk spreads that cost over many rows. So column stores use the log-structured idea again:
- New rows first go into a sorted, row-oriented store in memory.
- When enough pile up, they are merged with the column files on disk and written out as new files in bulk.
- Old files are never edited and new ones are written in one go, which suits object storage.
- Queries read both the column files and the recent rows in memory and combine them, so new data shows up right away.
Query execution: compilation and vectorization
A complex analytic query becomes a query plan: a chain of steps called operators, possibly spread across machines. The query planner decides which operators to use, in what order, and where each one runs. Operators do things like "keep rows where price > 15", "keep rows whose ID is in this set" (used for joins), or "combine several columns of the same row". With millions of rows, CPU time matters as much as disk reads. The naive approach acts like an interpreter: for every row, it checks the plan to decide what to do next, which is too slow. Two better approaches are used:
- Query compilation: generate code for this one query, compile it to machine code (often with LLVM), and run it over the column data in memory, much like a JIT compiler speeds up Java.
- Vectorized processing: still interpret the plan, but run ready-made operators over a whole batch of column values at a time instead of one row.
- For example, one operator turns "city = Pune" into a bitmap, another turns "item = tea" into a bitmap, and a bitwise AND gives tea sales in Pune.
Both are used in practice, and both win by fitting how modern CPUs work:
- They read memory in order, so the CPU cache misses less.
- They run tight loops with no function calls, which keeps the CPU pipeline busy and avoids wrong branch guesses.
- They use many threads and SIMD instructions, which handle several values per instruction.
- They work directly on compressed data, with no decoding and no extra copies.
Materialized views and data cubes
- A virtual view is a saved query that runs again each time you read it.
- A materialized view stores an actual copy of the results.
- When the base data changes, a materialized view must be updated, and some databases do this for you.
- So writes cost more, but repeated reads are fast.
Materialized aggregates store common totals like COUNT, SUM, AVG, MIN, and MAX. A data cube (or OLAP cube) is a grid of such totals grouped by several dimensions. For example, put months on one axis and stores on the other, and fill each cell with total sales. Adding up a row or a column drops one dimension, giving totals per store or per month. Real cubes often have more dimensions, such as product, store, promotion, and customer, and the idea works the same way.
- Upside: some queries become instant lookups, like yesterday's sales per store.
- Downside: a cube cannot answer questions about anything it did not group by, such as the share of sales from orders over $50.
So warehouses keep the raw data and use cubes only to speed up common queries.