SAPBR Technologies home

SAP in Brazil, explained

Dictionary Encoding

Now that memory can replace the hard disk, and CPUs are much faster than memory, memory is the new bottleneck.

Bottleneck illustration

Dictionary encoding is a compression technique that cuts the number of bits needed to represent data. Fewer bits means fewer operations to move data from main memory to the CPU, so the system runs faster.

Dictionary encoding is the foundation for many other compression techniques that run on encoded columns. It reduces redundancy by replacing each value with a shorter reference, using a value dictionary and an attribute vector. It’s the default compression applied to every column. I’ll cover compression more broadly later. First, let’s focus on dictionary encoding in a column store.

Dictionary encoding is simple. Not necessarily easy to learn, but easy to implement.

If you’ve read Prof. Hasso Plattner’s material, you know the example coming: the world population. It’s repetitive, but it’s the clearest one.

Dictionary encoding works column by column. Assume the world has 8 billion people (it was actually 7.49 billion in March 2017 and is expected to reach 9.6 billion by 2050). Now picture a table with these columns:

  • First name (fname)
  • Last name (lname)
  • Gender (gender)
  • Country (country)
  • City (city)
  • Date of birth (birthday)

Here’s sample data from this world_population table:

Sample rows of the world_population table

Dictionary encoding of a column

With dictionary encoding, each column is split into two structures:

  • Dictionary: stores every distinct value in the column once, each with a valueID (its position). The dictionary can be unsorted or sorted, which allows faster lookups.
  • Attribute vector: holds a valueID for every entry in the column. The valueID stands for the actual value recorded. It repeats as many times as that value appears in the column.
Dictionary and attribute vector for the fname column

In the attribute vector, valueID 33 appears twice (recIDs 59 and 62) because James appears twice in the fname column. The more often a value repeats, the bigger the compression benefit.

With a dictionary and an attribute vector per column, only a few bits are needed to encode fname. The column has low cardinality (among 8 billion first names there’s huge repetition), so you don’t write out every character each time a name repeats. Modern CPUs can build the output very quickly from the valueID and recID.

How do you retrieve the data?

Say the query lists everyone in the world whose first name is Michelle, in a dictionary-encoded database.

  1. Scan the dictionary for Michelle to get its valueID. Here, Michelle is valueID 34.
  2. Scan the attribute vector for every position with valueID 34.
  3. To output the results, replace valueID 34 in each position found with its dictionary value (Michelle).

Compression example with a dictionary

Each row of world_population takes 200 bytes:

Size of each world_population column in bytes

Without dictionary compression, the table takes:

8 billion rows x 200 bytes = 1.6 TB

Encoding fname. Using the binary logarithm, 23 bits are enough to store all 5 million distinct first names in the attribute vector: log2(5,000,000) rounded up = 23.

  • Uncompressed: 8 billion x 49 bytes = 392 billion bytes = 365.1 GB
  • Attribute vector: 8 billion x 23 bits = 184 billion bits = 23 billion bytes = 21.4 GB
  • Dictionary: 5 million x 49 bytes = 245 million bytes = 0.23 GB

Compressed, that’s 21.4 GB + 0.23 GB, about 17 times smaller than 365.1 GB.

Encoding gender. This column has only two distinct values, “m” and “f”.

  • Uncompressed: 8 billion x 1 byte = 7.45 GB
  • Attribute vector: 8 billion x 1 bit = 8 billion bits = 0.93 GB
  • Dictionary: 2 x 1 byte = 2 bytes

Compressed, that’s 0.93 GB + 2 bytes, about 8 times smaller than 7.45 GB.

Comparing fname and gender shows that the compression rate depends on the original data size, the number of distinct values and the table’s cardinality.

The biggest effect of dictionary encoding is that every operation on table data now runs on attribute vectors. That’s a huge gain, because CPUs are built to work on numbers, not characters.

Here’s the compression across the whole world_population table:

Compression results for every world_population column

First names: about 5 million distinct first names, which need 23 bits to encode. One bit encodes 2 values, 2 bits encode 4, 3 bits encode 8, 4 bits encode 16, and so on.

Note: 1 bit is either 1 or 0, so 2 values. 2 bits can be 00, 01, 10 or 11, so 4 values, and so on.

The experiment assumes the longest first name is 49 characters, so 49 bytes are reserved per first name.

Without dictionary encoding, the 8 billion first names need 365.10 GB.

Compressed, there are two structures. The dictionary holds the 5 million distinct names in 234 MB. The attribute vector has 8 billion entries of 23 bits each, for 21.42 GB.

So for first names alone: 365.10 GB without dictionary encoding, 21.65 GB with it. A compression factor of about 17.

Applying the same technique to all 6 columns and 8 billion rows, the table needs about 92 GB instead of 1.5 TB.

The takeaway: dictionary encoding means handling less data. Less data moves from memory to the CPU, so the same results come back faster.

World population map

Sources

  • Cache Conscious Column Organization in In-Memory Column Stores, David Schwalb, Jens Krüger and Hasso Plattner
  • A Course in In-Memory Data Management: The Inner Mechanics of In-Memory Databases, Hasso Plattner
  • The In-Memory Revolution: How SAP HANA Enables Business of the Future, Hasso Plattner
  • openHPI
  • openSAP

Comments

Leave a Reply