The MEMORY storage engine creates tables with contents that are stored in memory.
Formerly, these were known as HEAP tables. MEMORY is the preferred term, although HEAP remains supported for backward compatibility.
Each MEMORY table is associated with one disk file. The filename begins with the table name and has an extension of .frm to indicate that it stores the table definition.
1、heap对所有用户的连接是可见的,这使得它非常适合做缓存。
2、仅适合使用的场合。heap不允许使用xxxTEXT和xxxBLOB数据类型;只允许使用=和<=>操作符来搜索记录(不允 许&<、>、<=或>=)。
内存表的建立还有一些限制条件:
MEMORY tables cannot contain BLOB or TEXT columns.
HEAP不支持BLOB/TEXT列。
The server needs sufficient memory to maintain all MEMORY tables that are in use at the same time. 在同一时间需要足够的内存.
To free memory used by a MEMORY table when you no longer require its contents, you should execute DELETE or TRUNCATE TABLE, or remove the table altogether using DROP TABLE.为了释放内存,你应该执行DELETE FROM heap_table或DROP TABLE heap_table。
另外如何创建内存表?
创建内存表非常的简单,只需注明 ENGINE= MEMORY 即可:
注意:
当内存表中的数据大于max_heap_table_size设定的容量大小时,mysql会转换超出的数据存储到磁盘上,因此这是性能就大打折扣了,所 以我们还需要根据我们的实际情况调整max_heap_table_size,
例如在.cnf文件中[mysqld]的下面加入:
max_heap_table_size = 2048M
另外在建表语句中还可以通过MAX_ROWS来控制表的记录数。
CREATE TABLE `tablename` ( `columnName` varchar(256) NOT NUL) ENGINE=MEMORY DEFAULT CHARSET=utf-8 MAX_ROWS=100000000;
或 写成 TYPE=HEAP MAX_ROWS=100000000; 效果是一样的