Memory存储引擎使用存在内存中的内容来创建表,每个Memory表只实际对应一个磁盘文件,在磁盘中表现为.frm文件。Memory类型的表访问速度非常快,因为它的数据是放在内存中的,并且默认使用hash索引,但是一旦服务关闭,表中的数据就会丢失。
示例:create table memory_tab(id int)engine=memory;
因为memory在mysqld重启后数据就会丢失,为了获得稳定的数据源,可以在启动mysqld时加上—init-file选项,把insert into memory_tab select 或load data infile类似的语句放进去即可。
另外因为memory表是把数据放到内存中的,所以服务器需要有足够的内存来维持所有在同一时间使用的memory表,注意对连接mysqld的所有用户连接共享内存表中的数据,根据应用如果不是在所有连接间共享数据,最好在内存表中加入标志各个连接的ID。当不再需要memory数据时,要记得去删除自己连接的数据。
对于内存表的官方说明:
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 .fr