MENU
MEMORY
The MEMORY storage engine (formerly known as HEAP) stores table contents entirely in memory. Because the data is vulnerable to crashes, hardware issues, or power outages, MEMORY tables should be used only as temporary work areas or read-only caches for data pulled from other tables. See Storage Engines for how it compares to the others.MEMORY performance is limited by contention: single-thread execution and table-lock overhead when processing updates restrict scalability as load increases, especially for statement mixes that include writes. Despite processing entirely in memory, MEMORY tables are not necessarily faster than InnoDB tables on a busy server, for general-purpose queries, or under a read/write workload – the table locking involved in updates can slow down concurrent access from multiple sessions.
The maximum size of a MEMORY table is governed by the max_heap_table_size system variable, which defaults to 16MB.
| Feature | MEMORY |
| Storage limits | RAM (bounded by max_heap_table_size) |
| Transactions | No |
| Locking granularity | Table |
| MVCC | No |
| Geospatial data type support | No |
| Geospatial indexing support | No |
| B-tree indexes | Yes |
| T-tree indexes | No |
| Hash indexes | Yes |
| Full-text search indexes | No |
| Clustered indexes | No |
| Data caches | N/A |
| Index caches | N/A |
| Compressed data | No |
| Encrypted data | Yes |
| Cluster database support | No |
| Replication support | Yes |
| Foreign key support | No |
| Backup / point-in-time recovery | Yes |
| Query cache support | Yes |
| Update statistics for data dictionary | Yes |
-- A hash-indexed lookup table kept entirely in RAM
CREATE TABLE session_lookup (
session_id CHAR(32) PRIMARY KEY,
user_id INT NOT NULL,
INDEX (user_id) USING HASH
) ENGINE = MEMORY;
-- Cap the table's size explicitly for this session
SET SESSION max_heap_table_size = 64 * 1024 * 1024; -- 64MB
INSERT INTO session_lookup VALUES ('a1b2c3d4e5f6', 42);
SELECT user_id FROM session_lookup WHERE session_id = 'a1b2c3d4e5f6';
-- Data is lost on restart -- MEMORY tables are for caches/scratch space onlyQuery OK, 1 row affected
| user_id |
|---|
| 42 |