14.4. The MEMORY (HEAP) Storage Engine

MySQL 5.0

14.4. The MEMORY (HEAP) Storage Engine

The storage engine creates tables with contents that are stored in memory. Formerly, these were known as tables. is the preferred term, although remains supported for backward compatibility.

Each table is associated with one disk file. The filename begins with the table name and has an extension of to indicate that it stores the table definition.

To specify explicitly that you want to create a table, indicate that with an table option:

CREATE TABLE t (i INT) ENGINE = MEMORY;

The older term is supported as a synonym for for backward compatibility, but is the preferred term and is deprecated.

As indicated by the name, tables are stored in memory. They use hash indexes by default, which makes them very fast, and very useful for creating temporary tables. However, when the server shuts down, all rows stored in tables are lost. The tables themselves continue to exist because their definitions are stored in files on disk, but they are empty when the server restarts.

This example shows how you might create, use, and remove a table:

mysql> 
    ->     
    ->     
mysql> 
mysql> 

tables have the following characteristics:

  • Space for tables is allocated in small blocks. Tables use 100% dynamic hashing for inserts. No overflow area or extra key space is needed. No extra space is needed for free lists. Deleted rows are put in a linked list and are reused when you insert new data into the table. tables also have none of the problems commonly associated with deletes plus inserts in hashed tables.

  • tables can have up to 32 indexes per table, 16 columns per index and a maximum key length of 500 bytes.

  • The storage engine implements both and indexes. You can specify one or the other for a given index by adding a clause as shown here:

    CREATE TABLE lookup
        (id INT, INDEX USING HASH (id))
        ENGINE = MEMORY;
    CREATE TABLE lookup
        (id INT, INDEX USING BTREE (id))
        ENGINE = MEMORY;
    

    General characteristics of B-tree and hash indexes are described in Section 7.4.5, “How MySQL Uses Indexes”.

  • You can have non-unique keys in a table. (This is an uncommon feature for implementations of hash indexes.)

  • If you have a hash index on a table that has a high degree of key duplication (many index entries containing the same value), updates to the table that affect key values and all deletes are significantly slower. The degree of this slowdown is proportional to the degree of duplication (or, inversely proportional to the index cardinality). You can use a index to avoid this problem.

  • Columns that are indexed can contain values.

  • tables use a fixed-length row storage format.

  • tables cannot contain or columns.

  • includes support for columns.

  • You can use with tables. See Section 13.2.4.2, “ Syntax”.

  • tables are shared among all clients (just like any other non- table).

  • table contents are stored in memory, which is a property that tables share with internal tables that the server creates on the fly while processing queries. However, the two types of tables differ in that tables are not subject to storage conversion, whereas internal tables are:

    • If an internal table becomes too large, the server automatically converts it to an on-disk table. The size limit is determined by the value of the system variable.

    • tables are never converted to disk tables. To ensure that you don't accidentally do anything foolish, you can set the system variable to impose a maximum size on tables. For individual tables, you can also specify a table option in the statement.

  • The server needs sufficient memory to maintain all tables that are in use at the same time.

  • To free memory used by a table when you no longer require its contents, you should execute or , or remove the table altogether using .

  • If you want to populate a table when the MySQL server starts, you can use the option. For example, you can put statements such as or into this file to load the table from a persistent data source. See Section 5.2.1, “mysqld Command Options”, and Section 13.2.5, “ Syntax”.

  • If you are using replication, the master server's tables become empty when it is shut down and restarted. However, a slave is not aware that these tables have become empty, so it returns out-of-date content if you select data from them. When a table is used on the master for the first time since the master was started, a statement is written to the master's binary log automatically, thus synchronizing the slave to the master again. Note that even with this strategy, the slave still has outdated data in the table during the interval between the master's restart and its first use of the table. However, if you use the option to populate the table on the master at startup, it ensures that this time interval is zero.

  • The memory needed for one row in a table is calculated using the following expression:

    SUM_OVER_ALL_BTREE_KEYS( + sizeof(char*) × 4)
    + SUM_OVER_ALL_HASH_KEYS(sizeof(char*) × 2)
    + ALIGN(+1, sizeof(char*))
    

    represents a round-up factor to cause the row length to be an exact multiple of the pointer size. is 4 on 32-bit machines and 8 on 64-bit machines.

Additional resources