Rows, Columns, and the Shape of a Fast Query
A table looks the same in SQL whether its values sit together by record or by field. The physical arrangement, though, can determine how much memory a query reads, what the CPU can do with those bytes, and how much work is needed to assemble a result. That choice propagates through much of a database engine.
Imagine an event table with event_id, time, region, device, and latency_ms. An application request might fetch one event and display every field. An analytics query might average latency_ms across ten million events. The first asks for one complete record; the second asks for one value from every record. Both are ordinary SQL, but they make opposite demands of storage.
One logical table, two physical arrangements
In a row layout, adjacent bytes describe one event: its ID, time, region, device, and latency. The next bytes describe the next event. In a column layout, all IDs sit together, then all times, and so on. Position links fields back into records: the value at position i in each column belongs to event i.
The programming analogy is an array of structs versus a struct of arrays. Neither changes the meaning of the data. They change which values are near each other when memory or storage is read.
For a record lookup such as SELECT * FROM events WHERE event_id = 42, an index may locate the record first. A row layout then has its fields nearby. A column layout must visit several separate arrays and assemble the record. The lookup through an index is a separate cost; the layout question begins after it has identified the position.
For SELECT AVG(latency_ms) FROM events, the column layout can read a dense run of latencies. A row scan repeatedly steps over IDs, times, strings, and device values that the answer does not need. The processor transfers data in cache lines rather than fetching an isolated four-byte integer on demand. Even if the code never inspects the other fields, those bytes can consume cache and memory bandwidth.
An illustrative 48-byte record with a 4-byte latency would carry twelve times as many record bytes through a full scan as a packed latency array. This is a layout illustration, not a universal 12× query speedup: indexes, compression, prefetching, caching, and the rest of the query plan all matter.
Suppose eight latencies occupy 32 bytes. In a packed array they fit within one typical 64-byte cache line, subject to alignment. In a 48-byte record layout, those eight values are spread across 384 bytes of records and span roughly six cache lines. For a point lookup, the direction reverses: a complete record usually needs one or two adjacent lines, while fields scattered across separate columns require separate accesses. Strings can add another indirection in either arrangement, so these counts are deliberately simplified.
Batches change the execution loop
Layout also affects the way operators cooperate. A traditional iterator can ask its child operator for one tuple, test it, then request the next. If the arithmetic per tuple is cheap, virtual dispatch, type checks, and per-row coordination become substantial work.
A vectorized engine passes batches of same-type values between operators. A filter can examine a batch of latencies, produce selected positions, and hand those positions to an aggregation. It pays some coordination cost once per batch and runs tight loops over contiguous arrays. Keeping batches bounded helps intermediate results remain useful to the processor cache. DuckDB documents a default vector size of 2,048 tuples and several vector representations, including flat, constant, dictionary, and sequence vectors.
Dense values also make SIMD instructions practical: one instruction can operate on multiple lanes. A sum of 32-bit latencies might load many values at once and maintain several partial sums before combining them. SIMD is an opportunity, not a promise; the exact code depends on type conversion, null handling, compiler choices, and the machine. Apache Arrow’s format explicitly targets sequential access, constant-time indexed access, and vectorization-friendly buffers.
SIMD does not mean one instruction instantly sums a whole vector into one number. The lanes hold independent partial totals. As batches arrive, each lane accumulates the values assigned to it; a final horizontal reduction combines the lanes. A compiler may also unroll the loop and use several vector registers to hide instruction latency. With a row layout, the engine first has to gather the desired field from strided records, which can erase much of the benefit of a simple contiguous loop.
The batch boundary is a compromise. Passing one value between operators repeats dispatch overhead. Passing an entire huge column can make intermediate results too large for cache. A batch amortizes bookkeeping while giving the next operator a small, reusable working set. This is an execution strategy distinct from the on-disk format: an engine can read an encoded column page, produce batches, and run a chain of operators over them.
Compression is more than smaller files
Values in one column often share a type and a pattern. device might contain a handful of categories; an increasing event_id has small gaps; timestamps may share long prefixes. A storage engine can encode those patterns with dictionaries, runs, deltas, or bit packing. Mixed records offer fewer such uniform runs.
General compression can shrink arbitrary bytes, but a query may need to decompress an entire block to inspect a small part. Structured encodings can preserve useful operations. A dictionary-encoded device column, for example, can compare compact integer codes instead of repeatedly comparing strings. A sequence can sometimes be represented by a start and an increment. DuckDB’s constant, dictionary, and sequence vectors show how an engine can keep these representations during execution rather than eagerly expanding every value.
This does not mean every compressed value can be queried in place. The answer depends on the encoding and operation. A range predicate on deltas, a string search on dictionary codes, and an average of bit-packed integers have different costs. The valuable design principle is to decode only when the next operator needs decoded values.
Consider device values such as mobile, desktop, and tablet. A dictionary stores each string once and gives each row a small code. A filter for mobile can translate the target string to its code once and compare codes across the column. An increasing ID column can store a starting ID and compact increments. A repeated value can be represented as a run. These examples save storage, but they also retain enough structure to make some operations cheaper. Other operations, especially those that need the original strings or arbitrary values, still pay a decode cost.
Late materialization applies the same idea to records. Keep columns separate through filtering and aggregation, then reconstruct full rows only for the surviving result when needed. A query that returns a single count may never reconstruct a row at all.
Real files mix the two ideas
Purely separating each column for an entire table would make some operations awkward. Apache Parquet’s layout divides a file into row groups, places a chunk for each column inside each group, and divides chunks into pages. A reader can skip unrelated column chunks while keeping a bounded group of rows together. Metadata can also help skip groups that cannot match a predicate.
For example, if a row group’s timestamp minimum and maximum both lie outside a requested hour, the reader can skip that group. If the predicate is on latency_ms, it can avoid loading the region and device chunks altogether. Within a retained chunk, pages provide smaller units for decoding and reading. This nesting is why “columnar” does not mean a single endless byte stream for every field.
The distinction therefore has levels: logical table, row group, column chunk, page, encoded values, and in-memory batch. A database may use one layout on disk and another while processing a query. A transactional system might retain row-oriented primary storage while building a columnar replica or analytical index. Calling a product a “row store” or “column store” describes its center of gravity, not every byte it owns.
The cost of changing data
Rows are convenient for inserting or updating one complete record because the fields are colocated. A column layout may need to append to several streams, maintain their shared positions, and eventually rewrite compressed chunks. Systems often buffer new writes and merge them into optimized storage later. That improves read performance but adds background work, write amplification, and freshness considerations.
Likewise, a column scan does not win every analytical query. SELECT * over most records reads nearly every column and may pay a reconstruction cost. High-selectivity indexes can avoid scanning altogether. Narrow records, cache-resident data, and complex joins can change the balance. The useful question is not which layout is “faster” in isolation. It is which bytes a workload repeatedly asks to move and when it needs complete records.
The physical layout is the first decision in a chain. Grouping fields by record favors fetching and changing individual entities. Grouping values by field favors selective scans, batch processing, and pattern-aware compression. Once the query shape is clear, the engineering tradeoff becomes much easier to see.