日期:2014-05-16  浏览次数:20949 次

mysql优化: 内存表和临时表

由于直接使用临时表来创建中间表,其速度不如人意,因而就有了把临时表建成内存表的想法。但内存表和临时表的区别且并不熟悉,需要查找资料了。?
一开始以为临时表是创建后存在,当连接断开时临时表就会被删除,即临时表是存在于磁盘上的。而实际操作中发现临时表创建后去目录下查看发现并没有发现对应的临时表文件(未断开链接).因而猜测临时表的数据和结构都是存放在内存中,而不是在磁盘中.?
这样一想内存表不是也是存在在内存中吗,那么他和临时表有什么区别?他们的速度是什么样子??

查找了官方手册有以下的一些解释:?
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.?

由此可以看出来内存表会把表结构存放在磁盘上,把数据放在内存中。?
并做了以下实验:?
临时表?

Java代码??收藏代码
  1. mysql>?create?temporary?table?tmp1(id?int?not?null);??
  2. Query?OK,?0?rows?affected?(0.00?sec)??



Java代码??收藏代码
  1. mysql>?show?create?table?tmp1;??