加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql --The MEMORY Storage Engine--官方文档

发布时间:2020-12-11 23:56:03 所属栏目:MySql教程 来源:网络整理
导读:原文地址:http://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html The? ?storage engine (formerly known as? ) creates special-purpose tables with contents that are stored in memory. Because the data is vulnerable to crashes,hardwa
Storage limits Implemented in the server,rather than in the storage engine.

Implemented in the server,rather than in the storage engine.

When to Use MEMORY or MySQL Cluster.??Developers looking to deploy applications that use the?storage engine for important,highly available,or frequently updated data should consider whether MySQL Cluster is a better choice. A typical use case for the??engine involves these characteristics:

Operations involving transient,non-critical data such as session management or caching. When the MySQL server halts or restarts,the data in??tables is lost.

  • In-memory storage for fast access and low latency. Data volume can fit entirely in memory without causing the operating system to swap out virtual memory pages.

  • A read-only or read-mostly data access pattern (limited updates).

  • MySQL Cluster offers the same features as the??engine with higher performance levels,and provides additional features not available with?:

    Row-level locking and multiple-thread operation for low contention between clients.

  • Scalability even with statement mixes that include writes.

  • Optional disk-backed operation for data durability.

  • Shared-nothing architecture and multiple-host operation with no single point of failure,enabling 99.999% availability.

  • Automatic data distribution across nodes; application developers need not craft custom sharding or partitioning solutions.

  • Support for variable-length data types (including??and?) not supported by?.

  • For a white paper with more detailed comparison of the??storage engine and MySQL Cluster,see?. This white paper includes a performance study of the two technologies and a step-by-step guide describing how existing?users can migrate to MySQL Cluster.

    Performance Characteristics

    ?performance is constrained by contention resulting from single-thread execution and table lock overhead when processing updates. This limits scalability when load increases,particularly for statement mixes that include writes.

    Despite the in-memory processing for??tables,they are not necessarily faster than??tables on a busy server,for general-purpose queries,or under a read/write workload. In particular,the table locking involved with performing updates can slow down concurrent usage of??tables from multiple sessions.

    Depending on the kinds of queries performed on a??table,you might create indexes as either the default hash data structure (for looking up single values based on a unique key),or a general-purpose B-tree data structure (for all kinds of queries involving equality,inequality,or range operators such as less than or greater than). The following sections illustrate the syntax for creating both kinds of indexes. A common performance issue is using the default hash indexes in workloads where B-tree indexes are more efficient.

    Physical Characteristics of MEMORY Tables

    The??storage engine associates each table with one disk file,which stores the table definition (not the data). The file name begins with the table name and has an extension of?.

    ?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 use a fixed-length row-storage format. Variable-length types such as??are stored using a fixed length.

  • ?tables cannot contain??or??columns.

  • ?includes support for??columns.

  • Non-??tables are shared among all clients,just like any other non-?table.

  • DDL Operations for MEMORY Tables

    To create a??table,specify the clause??on the??statement.

    As indicated by the engine name,??tables are stored in memory. They use hash indexes by default,which makes them very fast for single-value lookups,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:

     CREATE TABLE test ENGINE=MEMORY
        ->     SELECT ip,SUM(downloads) AS down
        ->     FROM log_table GROUP BY ip;
    mysql> SELECT COUNT(ip),AVG(down) FROM test;
    mysql> DROP TABLE test;
    

    The maximum size of??tables is limited by the??system variable,which has a default value of 16MB. To enforce different size limits for??tables,change the value of this variable. The value in effect for?,or a subsequent??or?,is the value used for the life of the table. A server restart also sets the maximum size of existing??tables to the global?value. You can set the size for individual tables as described later in this section.

    Indexes

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

    For general characteristics of B-tree and hash indexes,see?.

    ?tables can have up to 64 indexes per table,16 columns per index and a maximum key length of 3072 bytes.

    If a??table hash index 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.

    ?tables can have nonunique keys. (This is an uncommon feature for implementations of hash indexes.)

    Columns that are indexed can contain??values.

    User-Created and Temporary Tables

    ?table contents are stored in memory,which is a property that??tables share with internal temporary 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 temporary tables are:

    If an internal temporary table becomes too large,the server automatically converts it to on-disk storage,as described in?.

  • User-created??tables are never converted to disk tables.

  • Loading Data

    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?,and?.

    MEMORY Tables and Replication

    A server's??tables become empty when it is shut down and restarted. If the server is a replication master,its slaves are not aware that these tables have become empty,so you see out-of-date content if you select data from the tables on the slaves. To synchronize master and slave??tables,when a??table is used on a master for the first time since it was started,a??statement is written to the master's binary log,to empty the table on the slaves also. The slave still has outdated data in the table during the interval between the master's restart and its first use of the table. To avoid this interval when a direct query to the slave could return stale data,use the??option to populate the??table on the master at startup.

    Managing Memory Use

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

    Memory is not reclaimed if you delete individual rows from a??table. Memory is reclaimed only when the entire table is deleted. Memory that was previously used for deleted rows is re-used for new rows within the same table. To free all the memory used by a??table when you no longer require its contents,execute??or?to remove all rows,or remove the table altogether using?. To free up the memory used by deleted rows,use??to force a table rebuild.

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

    max_length_of_key + sizeof(char*) * 4)
    + SUM_OVER_ALL_HASH_KEYS(sizeof(char*) * 2)
    + ALIGN(length_of_row+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.

    As mentioned earlier,the??system variable sets the limit on the maximum size of?tables. To control the maximum size for individual tables,set the session value of this variable before creating each table. (Do not change the global??value unless you intend the value to be used for?tables created by all clients.) The following example creates two??tables,with a maximum size of 1MB and 2MB,respectively:

     SET max_heap_table_size = 1024*1024;
    Query OK,0 rows affected (0.00 sec)
    

    mysql> <strong class="userinput">CREATE TABLE t1 (id INT,UNIQUE(id)) ENGINE = MEMORY;
    Query OK,0 rows affected (0.01 sec)

    mysql> <strong class="userinput">SET max_heap_table_size = 102410242;
    Query OK,0 rows affected (0.00 sec)

    mysql> <strong class="userinput">CREATE TABLE t2 (id INT,0 rows affected (0.00 sec)

    Both tables revert to the server's global??value if the server restarts.

    You can also specify a??table option in??statements for??tables to provide a hint about the number of rows you plan to store in them. This does not enable the table to grow beyond the?value,which still acts as a constraint on maximum table size. For maximum flexibility in being able to use?,set??at least as high as the value to which you want each?table to be able to grow.

    Additional Resources

    A forum dedicated to the??storage engine is available at?.

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    原文地址:http://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html

    The??storage engine (formerly known as?) creates special-purpose tables with contents that are stored in memory. Because the data is vulnerable to crashes,hardware issues,or power outages,only use these tables as temporary work areas or read-only caches for data pulled from other tables.

    Table?15.4??Storage Engine Features