All articles
Performance & memoryEngineering record

Database Engine Optimization Series — Part 1: Memory Optimization of Data Structures

Database Engine Optimization Series — Part 1: Memory Optimization of Data Structures

Preface

In database engines, designing the memory layout for complex data structures presents significantly more challenges than for primitive data types. This complexity stems mainly from the need to balance storage efficiency and computational performance. Memory is always a limited and valuable resource, yet computation time directly affects user experience. Therefore, achieving an efficient in-memory representation that also supports fast computation is a constant concern for engine developers.

This essay begins with a memory optimization case for the Array type in the Milvus database engine and shares practical insights into optimizing memory layouts within database cores.

Problem Background

During testing, our QA team discovered that after inserting 900 million records into a Milvus collecton, the system failed to load the data properly. The loading process ran out of memory after loading less than 10 million records.

At that time, the Grafana dashboard showed the following:

Figure 1View full-size image

The memory usage was observed to spike rapidly and hit the limit, while the schema used by QA was quite simple:

• ‘id’ (INT64, primary key)

• ‘float_vector’ (3-dimensional float vector)

• ‘array_varchar_1’ (an array of 10 strings)

The main data column, array_varchar_1, contains a fixed-length array of 10 identical string elements: [“0”, “0”, …, “0”]. A rough estimation of per-row memory usage is:

• INT64: 8 bytes

• 3 * float: 3 × 4 = 12 bytes

• array of 10 strings: approximately 10 bytes (since each is a short string “0”)

This totals roughly 30 bytes per row, which means 900 million rows should require about 25.2 GB of memory.

Considering that mmap was enabled, the actual memory usage at load time should be only 20–25% of that, i.e., around 5–7 GB in total. However, the system ran out of memory after loading only 10 million rows, which clearly deviates from expectations and indicates a serious inefficiency in the memory layout or data handling logic.

Memory Profiling

I conducted a local experiment by inserting 20 million rows of Array data and used jemalloc for profiling. The captured heap snapshot revealed the following:

Figure 2View full-size image

It was observed that 88.7% of the total 2704 MB heap memory was consumed by ArrayChunk::ConstructViews.

Root Cause Analysis

Milvus does follow the principle of contiguous storage with mmap for Array columns, and the data is indeed memory-mapped correctly. However, for variable-length types inner elements like string, contiguous storage still requires offset metadata to assist with reconstruction and access.

The actual memory-mapped layout for string arrays consists of two levels:

1. First-level metadata: describes the offset and length of each array in the column. For example, Array1 starts at offset 0 and contains 8 elements.

2. Second-level data: contains the offsets and actual string data of the inner array itself. Taking a simple example:

An array like Array1 = [“a”, “a”, …, “a”] (8 elements) requires:

• 8 × 8 = 64 bytes for internal string offsets (assuming each offset is int64_t)

• 8 bytes for the actual string content (each “a” is one byte)

• Total: 72 bytes

Therefore, the start offset for Array2 must be 72.

Figure 3View full-size image

However, beyond the mmap’ed data, Milvus also constructs a read-only ArrayView for each array entry.

As illustrated above, each ArrayView holds:

• a pointer to the mmap’ed data block,

• the number of elements in the array, and

• a copied list of offsets to access individual elements.

This design was originally intended to accelerate array expression evaluation. For example, with an expression like “arr[3] = ‘b’”, the execution engine can quickly locate the offset of the third element and use pointer arithmetic to fetch the value directly.

However, the downside is that ArrayView copies the offsets list, effectively doubling the memory usage for each array.

A straightforward memory optimization is to eliminate the duplicated offsets copy in ArrayView, and instead reference the original offsets directly from the mmap file.

Figure 4View full-size image

As shown in the figure above, ArrayView no longer stores a list of offsets, but instead directly stores a pointer to the offsets. In this way, each ArrayView saves 8 × 8 = 64 bytes of memory. Additionally, replacing the original 24-byte std::vector with an 8-byte pointer saves another 16 bytes, resulting in a total memory saving of 80 bytes per array.

Effect Validation

Open-source PR link: https://github.com/milvus-io/milvus/pull/40206

After recompiling and running Milvus, we captured a new jemalloc profile. As shown in the figure below, for the same 20 million rows of data, the memory consumed by ArrayChunk::ConstructViews dropped from approximately 2534 MB to 512 MB.

Figure 5View full-size image

Conclusion

This article introduced the memory optimization process in the Milvus database engine. I primarily used jeprof to profile memory usage, and reduced unnecessary memory consumption by leveraging mmap-based memory mapping.

In relatively mature data systems, static memory leaks in data structures are rare; instead, the design of data structure layout itself is often the key focus. Choosing the right memory profiling tools is an essential part of optimization work.

Additional Notes

The profiling method described in this article requires both jemalloc and enable_prof_jemalloc to be enabled.

— MrPresent-Han