SAPBR Technologies home

SAP in Brazil, explained

In-Memory Data Management – Part II

This follows Part I and covers chapter II of the learning map, foundations of database storage:

  1. Dictionary encoding and compression
  2. Data layout: row, column, hybrid
  3. Partitioning

1. Dictionary encoding and compression

CPUs are now much faster than memory, so with data no longer read from disk, memory is the new bottleneck. Dictionary encoding reduces the bits needed to represent data, so less moves from main memory to the CPU and the system runs faster.

Compression

Dictionary encoding is the default compression for every column and the base for other compression techniques on encoded columns. It replaces repeated values with short references through a dictionary and an attribute vector. The idea is simple and easy to implement, even if it takes a moment to learn.

Like Prof. Hasso Plattner’s material, I’ll use the world population as the example.

World population map

Assume 8 billion people (7.51 billion in July 2017, 9.6 billion expected by 2050) in a table with first name (fname), last name (lname), gender, country, city and date of birth (birthday).

Sample data in the world_population table
Sample data in the world_population table

How a column is encoded

Each column becomes two structures:

  • Dictionary: every distinct value in the column, each with a valueID (its position). It can be unsorted or sorted; sorted dictionaries are fast for lookups, inserts and joins.
  • Attribute vector: one valueID per row, pointing to the dictionary. An integer replaces the string, repeated as often as the value appears.
Dictionary and attribute vector

In the example, valueID 33 appears twice (recIDs 59 and 62) because James appears twice in fname. The more values repeat, the bigger the compression. First names have low cardinality across 8 billion rows, so a few bits per row are enough, and modern CPUs rebuild the output quickly from valueID and recID.

Reading the data

To list everyone named Michelle:

  1. Scan the dictionary for Michelle and get its valueID (34 here).
  2. Scan the attribute vector for every position with valueID 34.
  3. Replace valueID 34 with Michelle in the output.

Dictionary entries are sorted numerically or lexicographically.

How much it saves

Each row of world_population takes 200 bytes.

Tuple size

Uncompressed: 8 billion rows × 200 bytes = 1.6 TB.

First names. There are about 5 million distinct first names; log2(5,000,000) rounds up to 23 bits. With the longest name at 49 characters (49 bytes):

CalculationSize
Uncompressed8 billion × 49 bytes365.1 GB
Attribute vector8 billion × 23 bits21.4 GB
Dictionary5 million × 49 bytes0.23 GB
Compressed total21.65 GB, about 17× smaller

Gender. Two values, m and f:

CalculationSize
Uncompressed8 billion × 1 byte7.45 GB
Attribute vector8 billion × 1 bit0.93 GB
Dictionary2 × 1 byte2 bytes
Compressed totalabout 8× smaller

The compression rate depends on the original data size, the number of distinct values and the table’s cardinality. Most importantly, every operation now runs on integers in the attribute vectors, and CPUs are built to work with numbers, not characters.

world_population overall compression

1 bit holds 2 values (0 or 1), 2 bits hold 4 (00, 01, 10, 11), 3 bits hold 8, and so on.

Applied to all 6 columns and 8 billion rows, the table shrinks from about 1.5 TB to about 92 GB. Less data moves from memory to the CPU, so the same results come back faster.

2. Data layout: row, column, hybrid

Main memory is one-dimensional and can be read sequentially or randomly, but access costs aren’t constant: they depend on the access pattern. The larger the area read, the more cache misses, so data should be processed in cache-friendly chunks.

Take a small table where every value is a string:

Sample table with three rows

The database turns the two-dimensional table into a one-dimensional series of bytes. A row layout stores each row’s attributes together: “1, John Jones, Australia, Sydney; 2, Bryan Quinn, USA, Washington; 3, Lauren Falks, Germany, Berlin”. A column layout stores each column together: “1, 2, 3; John Jones, Bryan Quinn, Lauren Falks; Australia, USA, Germany; Sydney, Washington, Berlin”.

Column stores are faster for reading many rows across few columns, because each column is read sequentially. Row stores are faster for single-record operations and inserts. That’s why OLTP traditionally uses rows and data warehouses (OLAP) use columns. In spreadsheet terms: a row layout fills row 1, then row 2; a column layout fills column A, then column B.

Row layout

Rebuilding a record is cheap, but scanning one attribute is expensive. To get James’s full record, go to his row. To count men and women in the world, jump from record to record collecting each gender.

Column operation

Column operation on a row layout

Row operation

Row operation on a row layout

Column layout

Data is stored by attribute. All the first names sit together, then all the genders, so scanning a column is easy. Rebuilding James’s full record means jumping between columns.

Column operation

Column operation on a column layout

Row operation

Row operation on a column layout

Row layouts still win in some cases, but for enterprise systems the column layout comes out ahead:

  • Enterprise workloads are mostly reads and set processing.
  • Memory keeps growing, but compression still matters to keep data in memory and reduce data moved between storage, memory and CPU.
  • Dictionary encoding works on both layouts, but prefix, run-length, cluster and indirect encoding rely on columns.
  • Fast sequential column scans compute aggregates on the fly, so stored aggregates, redundancy and complexity go away.

Hybrid layout

Every workload is different. A hybrid layout stores some attributes by column and groups others by row, based on the workload; layout algorithms can calculate the best mix. Attributes used together, like quantity and unit of measure, or payment terms and conditions, perform better stored together. If ID and name are usually read together: “1, John Jones; 2, Bryan Quinn; 3, Lauren Falks; Australia, USA, Germany; Sydney, Washington, Berlin”.

Hybrid layouts can reduce cache misses, but raise new questions: how to find the best layout and how to adapt when the workload changes.

Data virtualization

3. Partitioning

Partitioning splits a logical database into independent, non-overlapping datasets. It’s an easy way to get data-level parallelism and faster queries. Finding the optimal partitioning can’t be solved in polynomial time, so the challenge is choosing a good one.

Vertical partitioning splits the data into attribute groups, usually repeating the primary key, across two or more tables. Attributes used together should stay together to speed up joins. It’s rare in row stores, because it breaks the row-wise idea, but column stores support it naturally: each column is a potential partition.

Vertical partitioning

Horizontal partitioning splits the table into row groups by a condition. It’s more common in row stores. Four types:

Range: a partition key (one or more columns) decides where each row goes. For example, customers by date of birth, about 25 years per partition for four partitions. The best key depends on the workload.

Range partitioning

Round robin: no key; rows go to each partition in turn. Distribution is even, which helps load balancing, but some rows are read far more than others, so the workload may not be even, and co-location benefits are lost because placement depends only on insert order.

Round robin partitioning

Hash: a hash function assigns each row to a partition. The challenge is a good hash function. In the example, countries of very different sizes are hashed into fewer keys to balance the partitions.

Hash partitioning

Semantic: uses application knowledge. For example, partitioning sales order tables by life-cycle step: creation, purchase, release, delivery, dunning. The application decides whether to split active data only or active and history; the database only provides the split.

The right strategy depends on the use case and what you’re optimizing for.

That’s the end of chapter II. The next part covers chapter III, in-memory database operators, from the outline in Part I.

Comments

One response to “In-Memory Data Management – Part II”

  1. In-Memory Data Management – Part I – SAP BRAZIL & S/4HANA Avatar

    […] II – Foundations of database storage techniques […]

    Like

Leave a Reply