MariaDB’s MEMORY engine is designed for incredibly fast data access by storing tables entirely in RAM.

Let’s see it in action. Imagine a simple use case: caching frequently accessed, small datasets that don’t require disk persistence.

-- Create a table using the MEMORY engine
CREATE TABLE sessions (
    session_id VARCHAR(255) PRIMARY KEY,
    user_id INT,
    login_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=MEMORY;

-- Insert some data
INSERT INTO sessions (session_id, user_id) VALUES
('abc123xyz', 101),
('def456uvw', 102);

-- Query the data - this will be lightning fast
SELECT * FROM sessions WHERE session_id = 'abc123xyz';

The output will be instantaneous, as the data is read directly from memory.

The core problem MEMORY solves is the I/O bottleneck inherent in disk-based storage. When data is in RAM, the latency for reads and writes drops from milliseconds (disk) to nanoseconds (memory). This makes it ideal for temporary tables, session management, caching layers, or any scenario where speed trumps durability.

Internally, MEMORY tables are implemented as hash tables or B-trees, depending on the index type. For primary keys, it typically uses a hash index, allowing for O(1) average-case lookups. This is crucial for its performance. Unlike disk-based engines, there’s no overhead for disk seeks, block reads, or writes.

You control MEMORY tables primarily through their definition and the indexes you create. ENGINE=MEMORY is the key directive. For performance, understanding the indexing is paramount. Hash indexes (the default for PRIMARY KEY and UNIQUE indexes) offer the fastest lookups. B-tree indexes are also supported and can be more efficient for range queries, though still faster than disk-based equivalents due to being in memory.

A surprising limitation, often overlooked until it bites, is that MEMORY tables don’t support FULLTEXT indexes. This means you can’t use them for full-text searching directly; you’d need to offload that to a disk-based table or a specialized search engine. Additionally, if your MEMORY table grows beyond available RAM, MariaDB will start swapping to disk, negating the performance benefits and potentially leading to significant slowdowns or even out-of-memory errors.

The most common way to manage the size of MEMORY tables and prevent them from consuming excessive RAM is by setting max_heap_table_size. This system variable, along with tmp_table_size (which affects temporary tables created implicitly by complex queries), dictates the maximum memory a single MEMORY table can occupy. If a MEMORY table exceeds these limits, MariaDB will automatically convert it to a MyISAM (or InnoDB, depending on configuration) table on disk, which is a silent but performance-killing operation.

The next logical step after mastering in-memory tables is exploring how to leverage them effectively within complex query execution plans, especially when they interact with disk-based tables.

Want structured learning?

Take the full Mariadb course →