Notes

Part 8 of 10

Data storage for analytics

Data warehouses usually use the relational model, since SQL is a good fit for analytic queries. A warehouse and an OLTP database may both speak SQL, but their insides differ because their query patterns differ. Most vendors now focus on one or the other. Hybrid (HTAP) products exist, but more and more they are really two separate engines behind one SQL interface.

Cloud data warehouses

Cloud warehouses are built on cloud services: object storage for the data and serverless compute for the queries.

  • They connect easily to other cloud services, such as automatic loading of logs and data processing frameworks.
  • Compute is separate from storage, so each can grow or shrink on its own, which makes them very elastic.

Open source warehouses have broken into separate parts, as data moved into data lakes on object storage:

PartJob
Query engineParses SQL, plans the query, and runs it, often as many parallel tasks across machines
Storage formatDecides how a table's rows become bytes in a file, in a way other tools can read too
Table formatFiles in the storage format are never edited, so this tracks which files make up a table and its schema, and adds inserts, deletes, querying old versions, cleanup, and even transactions
Data catalogTracks which tables exist and handles create, rename, and drop, usually as a separate service so discovery and governance tools can read the metadata

Column-oriented storage

Warehouses often use a star schema: a huge fact table, with foreign keys pointing to smaller dimension tables. The fact table holds most of the data, sometimes trillions of rows and petabytes. It may have over 100 columns, yet a typical query touches only 4 or 5. Asking for every column at once is rare in analytics.

  • Row-oriented storage keeps all values of one row together.
  • Most OLTP databases work this way, and so do document databases.
  • Even with indexes, a row store must load whole wide rows from disk, parse them, and then filter, which is slow.
  • Column-oriented storage keeps all values of one column together.
  • A query then reads only the columns it needs.

Every column stores rows in the same order, so row 7 is the 7th value in every column. In practice the table is cut into blocks of thousands to millions of rows, and columns are stored separately inside each block. Blocks often follow time ranges, so a query on last month loads only matching blocks and only the columns it needs. This works for non-relational data too, such as nested documents.

Do not mix this up with wide-column databases. In a wide-column database, rows can have thousands of different columns, but data is still stored row by row.

Column compression

Columns compress well, because the same values repeat a lot. Compression cuts both disk reads and network traffic. Each column can use the compression method that suits it.

  • Bitmap encoding: make one bitmap per distinct value, with one bit per row, set to 1 if that row has the value.
  • This works because a column usually has far fewer distinct values than there are rows, like a few thousand product IDs across billions of sales.
  • Run-length encoding: most of those bitmaps are mostly zeros, so store the lengths of runs, like "900 zeros, 3 ones, 4,000 zeros", instead of every bit.
  • Filters as bit math: color IN ('red', 'blue') is a bitwise OR of two bitmaps.
  • color = 'red' AND size = 'L' is a bitwise AND of two bitmaps, which works because all columns share the same row order.

Sort order in column storage

Rows can be stored in the order they arrive, which is easy since you just append to each column. Or they can be sorted so the order acts like an index. You cannot sort each column on its own, or rows would stop lining up. Instead you sort whole rows and then store them by column.

  • Pick the sort columns from common queries.
  • If most queries filter by date, put date first, so "last 7 days" reads one small stretch.
  • A second sort key orders rows that share the first key, such as region within each date, which groups related rows together.
  • Sorting helps compression: the first sort column has long runs of the same value, so run-length encoding can shrink billions of rows to a few KB.
  • Later sort keys are more mixed and compress less, but the overall gain is still worth it.