Memory Storage Engine

Last updated: October 26, 2023

The MEMORY storage engine, also known as the HEAP storage engine, provides a high-performance, in-memory table type. Data stored in MEMORY tables is held in RAM, which allows for very fast data retrieval and manipulation. This engine is ideal for temporary tables, caching, or situations where data persistence is not a primary concern.

Key Characteristics

When to Use the MEMORY Engine

Note: Due to its in-memory nature, the size of MEMORY tables is limited by the available RAM on the server. Exceeding available memory can lead to performance degradation or server instability.

Table Creation Example

To create a table using the MEMORY storage engine, you specify ENGINE=MEMORY during table creation:

CREATE TABLE sessions (
    session_id VARCHAR(255) NOT NULL PRIMARY KEY,
    user_id INT,
    login_time DATETIME,
    data TEXT -- Note: TEXT is not fully supported, see limitations
) ENGINE=MEMORY;

Warning: While the syntax above might appear to work, the TEXT data type and other large object types (like BLOB) are not fully supported by the MEMORY engine. You should generally avoid them. If you need to store such data, consider a disk-based engine.

Supported Data Types

The MEMORY engine supports most standard numeric, string, and date/time data types. However, it does not support BLOB, TEXT, or spatial data types.

Category Supported Types Unsupported Types
Numeric TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, DECIMAL, FLOAT, DOUBLE N/A
String CHAR, VARCHAR, BINARY, VARBINARY, ENUM, SET TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT
Date/Time DATE, TIME, DATETIME, TIMESTAMP, YEAR N/A
Other BOOLEAN (as TINYINT(1)) Spatial types

Index Types

The MEMORY engine supports the following index types:

By default, if you create a PRIMARY KEY or a UNIQUE index on a MEMORY table, it will be created as a HASH index unless you explicitly specify USING BTREE.

-- HASH index (default for PRIMARY KEY)
CREATE TABLE products (
    product_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100)
) ENGINE=MEMORY;

-- BTREE index
CREATE TABLE logs (
    log_time DATETIME,
    message VARCHAR(255),
    INDEX idx_log_time (log_time) USING BTREE
) ENGINE=MEMORY;

Limitations and Considerations

Tuning MEMORY Tables

Configuration parameters related to MEMORY tables often involve memory allocation and performance tuning. For example, the max_heap_table_size system variable limits the maximum size of MEMORY tables that can be created or altered.

-- Show current limit
SHOW VARIABLES LIKE 'max_heap_table_size';

-- Set a new limit (e.g., 1GB)
SET GLOBAL max_heap_table_size = 1073741824;

It's important to monitor memory usage and adjust these settings cautiously to avoid impacting overall server stability.

Tip: For optimal performance, use HASH indexes on MEMORY tables when performing equality lookups. Use BTREE indexes for range queries or when order matters.

The MEMORY storage engine is a powerful tool for specific use cases where speed is paramount and data durability is secondary. Understanding its limitations is key to using it effectively.