mysql --The MEMORY Storage Engine--官方文档
原文地址:http://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html
The MEMORY
storage engine (formerly known as HEAP
) 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 MEMORY
Storage Engine Features
Storage limits | RAM | 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[a] | Yes |
Cluster database support | No | Replication support[b] | Yes | Foreign key support | No |
Backup / point-in-time recovery[c] | Yes | Query cache support | Yes | Update statistics for data dictionary | Yes |
[a] Implemented in the server (via encryption functions), rather than in the storage engine. [b] Implemented in the server, rather than in the storage engine. [c] 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 MEMORY
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 MEMORY
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
MEMORY
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 MEMORY
engine with higher performance levels, and provides additional features not available with MEMORY
:
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
BLOB
andTEXT
) not supported byMEMORY
.
For a white paper with more detailed comparison of the MEMORY
storage engine and MySQL Cluster, see Scaling Web Services with MySQL Cluster: An Alternative to the MySQL Memory Storage Engine. This white paper includes a performance study of the two technologies and a step-by-step guide describing how existing MEMORY
users can migrate to MySQL Cluster.
Performance Characteristics
MEMORY
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 MEMORY
tables, they are not necessarily faster than InnoDB
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 MEMORY
tables from multiple sessions.
Depending on the kinds of queries performed on a MEMORY
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 MEMORY
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 .frm
.
MEMORY
tables have the following characteristics:
Space for
MEMORY
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.MEMORY
tables also have none of the problems commonly associated with deletes plus inserts in hashed tables.MEMORY
tables use a fixed-length row-storage format. Variable-length types such asVARCHAR
are stored using a fixed length.MEMORY
includes support forAUTO_INCREMENT
columns.Non-
TEMPORARY
MEMORY
tables are shared among all clients, just like any other non-TEMPORARY
table.
DDL Operations for MEMORY Tables
To create a MEMORY
table, specify the clause ENGINE=MEMORY
on the CREATE TABLE
statement.
CREATE TABLE t (i INT) ENGINE = MEMORY;
As indicated by the engine name, MEMORY
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 MEMORY
tables are lost. The tables themselves continue to exist because their definitions are stored in .frm
files on disk, but they are empty when the server restarts.
This example shows how you might create, use, and remove a MEMORY
table:
mysql>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 MEMORY
tables is limited by the max_heap_table_size
system variable, which has a default value of 16MB. To enforce different size limits for MEMORY
tables, change the value of this variable. The value in effect for CREATE TABLE
, or a subsequent ALTER TABLE
or TRUNCATE TABLE
, is the value used for the life of the table. A server restart also sets the maximum size of existing MEMORY
tables to the globalmax_heap_table_size
value. You can set the size for individual tables as described later in this section.
Indexes
The MEMORY
storage engine supports both HASH
and BTREE
indexes. You can specify one or the other for a given index by adding a USING
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;
For general characteristics of B-tree and hash indexes, see Section 8.3.1, “How MySQL Uses Indexes”.
MEMORY
tables can have up to 64 indexes per table, 16 columns per index and a maximum key length of 3072 bytes.
If a MEMORY
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 BTREE
index to avoid this problem.
MEMORY
tables can have nonunique keys. (This is an uncommon feature for implementations of hash indexes.)
Columns that are indexed can contain NULL
values.
User-Created and Temporary Tables
MEMORY
table contents are stored in memory, which is a property that MEMORY
tables share with internal temporary tables that the server creates on the fly while processing queries. However, the two types of tables differ in thatMEMORY
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 Section 8.4.4, “How MySQL Uses Internal Temporary Tables”.
User-created
MEMORY
tables are never converted to disk tables.
Loading Data
To populate a MEMORY
table when the MySQL server starts, you can use the --init-file
option. For example, you can put statements such as INSERT INTO ... SELECT
or LOAD DATA INFILE
into this file to load the table from a persistent data source. See Section 5.1.3, “Server Command Options”, and Section 13.2.6, “LOAD DATA INFILE Syntax”.
MEMORY Tables and Replication
A server's MEMORY
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 MEMORY
tables, when a MEMORY
table is used on a master for the first time since it was started, a DELETE
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 --init-file
option to populate the MEMORY
table on the master at startup.
Managing Memory Use
The server needs sufficient memory to maintain all MEMORY
tables that are in use at the same time.
Memory is not reclaimed if you delete individual rows from a MEMORY
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 MEMORY
table when you no longer require its contents, execute DELETE
orTRUNCATE TABLE
to remove all rows, or remove the table altogether using DROP TABLE
. To free up the memory used by deleted rows, use ALTER TABLE ENGINE=MEMORY
to force a table rebuild.
The memory needed for one row in a MEMORY
table is calculated using the following expression:
SUM_OVER_ALL_BTREE_KEYS(max_length_of_key
+ sizeof(char*) * 4)
+ SUM_OVER_ALL_HASH_KEYS(sizeof(char*) * 2)
+ ALIGN(length_of_row
+1, sizeof(char*))
ALIGN()
represents a round-up factor to cause the row length to be an exact multiple of the char
pointer size.sizeof(char*)
is 4 on 32-bit machines and 8 on 64-bit machines.
As mentioned earlier, the max_heap_table_size
system variable sets the limit on the maximum size of MEMORY
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 max_heap_table_size
value unless you intend the value to be used forMEMORY
tables created by all clients.) The following example creates two MEMORY
tables, with a maximum size of 1MB and 2MB, respectively:
mysql>SET max_heap_table_size = 1024*1024;
Query OK, 0 rows affected (0.00 sec) mysql>CREATE TABLE t1 (id INT, UNIQUE(id)) ENGINE = MEMORY;
Query OK, 0 rows affected (0.01 sec) mysql>SET max_heap_table_size = 1024*1024*2;
Query OK, 0 rows affected (0.00 sec) mysql>CREATE TABLE t2 (id INT, UNIQUE(id)) ENGINE = MEMORY;
Query OK, 0 rows affected (0.00 sec)
Both tables revert to the server's global max_heap_table_size
value if the server restarts.
You can also specify a MAX_ROWS
table option in CREATE TABLE
statements for MEMORY
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 themax_heap_table_size
value, which still acts as a constraint on maximum table size. For maximum flexibility in being able to use MAX_ROWS
, set max_heap_table_size
at least as high as the value to which you want eachMEMORY
table to be able to grow.
Additional Resources
A forum dedicated to the MEMORY
storage engine is available at http://forums.mysql.com/list.php?92.
mysql --The MEMORY Storage Engine--官方文档的更多相关文章
- MySQL 标识符到底区分大小写么——官方文档告诉你
最近在阿里云服务器上部署一个自己写的小 demo 时遇到一点问题,查看 Tomcat 日志后定位到问题出现在与数据库服务器交互的地方,执行 SQL 语句时会返回 指定列.指定名 不存在的错误.多方查证 ...
- Mysql优化(出自官方文档) - 第八篇(索引优化系列)
目录 Mysql优化(出自官方文档) - 第八篇(索引优化系列) Optimization and Indexes 1 Foreign Key Optimization 2 Column Indexe ...
- Mysql优化(出自官方文档) - 第十篇(优化InnoDB表篇)
Mysql优化(出自官方文档) - 第十篇(优化InnoDB表篇) 目录 Mysql优化(出自官方文档) - 第十篇(优化InnoDB表篇) 1 Optimizing Storage Layout f ...
- Mysql优化(出自官方文档) - 第三篇
目录 Mysql优化(出自官方文档) - 第三篇 1 Multi-Range Read Optimization(MRR) 2 Block Nested-Loop(BNL) and Batched K ...
- Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇)
目录 Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇) 1 Optimizing Data Size 2 Optimizing MySQL Data Types 3 Optimizing ...
- Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇)
Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇) 目录 Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇) 1 Internal Locking Methods Row-Leve ...
- Mysql优化(出自官方文档) - 第一篇(SQL优化系列)
Mysql优化(出自官方文档) - 第一篇 目录 Mysql优化(出自官方文档) - 第一篇 1 WHERE Clause Optimization 2 Range Optimization Skip ...
- MySQL8.0.28安装教程全程参考MySQL官方文档
前言 为了MySQL8.0.28安装教程我竟然在MySQL官方文档逛了一天,至此献给想入门MySQL8.0的初学者.以目前最新版本的MySQL8.0.28为示例进行安装与初步使用的详细讲解,面向初学者 ...
- 看MySQL官方文档的示例SQL有感
[背景] 周末比较闲,我这个人又没有什么爱好,当然了读书除外:前一些天我一个同事说:“你一个dba想去写一本“django”书,合适吗?” 我想也是,一个人不能忘了本,所以MySQL还是要好好的搞一搞 ...
随机推荐
- 如何给10^7个数据量的磁盘文件排序--bitset
题目: 输入:给定一个文件,里面最多含有n个不重复的正整数(也就是说可能含有少于n个不重复正整数),且其中每个数都小于等于n,n=10^7.输出:得到按从小到大升序排列的包含所有输入的整数的列表. 分 ...
- JSON2 源代码
/* json2.js 2014-02-04 Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. See ht ...
- MFC特定函数的应用20160720(SystemParametersInfo,GetWindowRect,WriteProfileString,GetSystemMetrics)
1.SystemParametersInfo函数可以获取和设置数量众多的windows系统参数 MFC中可以用 SystemParametersInfo(……) 函数来获取和设置系统信息,如下面例子所 ...
- 我的web前端修炼之路从此开始
看过一篇文章,上面说过要想学习一门新技术,从什么时候开始都是不晚的.但对于一名大四的学生,只会一点简单的网页架构,只懂得HTML,CSS,JavaScript简单的一点皮毛,却怎么也说不过去.但也是这 ...
- ASP.NET MVC3 系列教程 – Web Pages 1.0
http://www.cnblogs.com/highend/archive/2011/04/14/aspnet_mvc3_web_pages.html I:Web Pages 1.0中以“_”开头的 ...
- VS20xx下项目开发目录管理方法
在VS20XX之后项目管理使用解决方案(solution)管理一个大的开发工程中多个项目(Project). 以下目录配置与工程名称无关,适用于一般的工程组织过程. 对于一般的生成可执行程序的工程,使 ...
- mysql 查看锁表解锁
-- 查看那些表锁到了show OPEN TABLES where In_use > 0;-- 查看进程号show processlist;--删除进程 kill 42236:
- Windwos Server 2008: 当网卡有多个IP地址时,如何指定缺省地址?
这实际是一个当应用向外发起连接时,协议栈对源IP地址的选择问题.如果你的应用没有显式绑定一个本地地址,协议栈会选择一个"最佳"的本地地址来使用. 从 Vista 之后这个选择策略发 ...
- bash里,echo对换行符的处理
echo -e "#include <stdio.h>\nint main()\n{\n printf(\"hello world\\\n\");\n ret ...
- PHP网站简单架构 – 单独跑php-fpm
这个架构比较简单,不做过多的说明 前端1台Nginx:负载均衡+nfs 中间2台php:php-fpm 后端1台数据库:MySQL 安装略,参考<lnmp最新源码一键安装包> 192.16 ...