mysql 临时表 heap_mysql优化: 内存表和临时表
由于直接使用臨時(shí)表來創(chuàng)建中間表,其速度不如人意,因而就有了把臨時(shí)表建成內(nèi)存表的想法。但內(nèi)存表和臨時(shí)表的區(qū)別且并不熟悉,需要查找資料了。
一開始以為臨時(shí)表是創(chuàng)建后存在,當(dāng)連接斷開時(shí)臨時(shí)表就會(huì)被刪除,即臨時(shí)表是存在于磁盤上的。而實(shí)際操作中發(fā)現(xiàn)臨時(shí)表創(chuàng)建后去目錄下查看發(fā)現(xiàn)并沒有發(fā)現(xiàn)對(duì)應(yīng)的臨時(shí)表文件(未斷開鏈接).因而猜測(cè)臨時(shí)表的數(shù)據(jù)和結(jié)構(gòu)都是存放在內(nèi)存中,而不是在磁盤中.
這樣一想內(nèi)存表不是也是存在在內(nèi)存中嗎,那么他和臨時(shí)表有什么區(qū)別?他們的速度是什么樣子?
查找了官方手冊(cè)有以下的一些解釋:
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.
由此可以看出來內(nèi)存表會(huì)把表結(jié)構(gòu)存放在磁盤上,把數(shù)據(jù)放在內(nèi)存中。
并做了以下實(shí)驗(yàn):
臨時(shí)表
mysql> create temporary table tmp1(id int not null);
Query OK, 0 rows affected (0.00 sec)
mysql> show create table tmp1;
+-------+----------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------+
| tmp1 | CREATE TEMPORARY TABLE `tmp1` ( `id` int(11) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
內(nèi)存表
mysql> create table tmp2(id int not null) TYPE=HEAP;
Query OK, 0 rows affected (0.00 sec)
mysql> show create table tmp2;
+-------+------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------+
| tmp2 | CREATE TABLE `tmp2` (
`id` int(11) NOT NULL
) ENGINE=MEMORY DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
可以看出來臨時(shí)表和內(nèi)存表的ENGINE 不同,臨時(shí)表默認(rèn)的是MyISAM,而內(nèi)存表是MEMORY .去數(shù)據(jù)庫目錄查看,發(fā)現(xiàn)tmp2.frm而沒有tmp1表的任何文件。看來實(shí)際情況是符合官方解釋的。
那么速度方面呢(即MyISAM和MEMORY之間的區(qū)別)?
實(shí)驗(yàn)開始:
實(shí)現(xiàn)手段:對(duì)基于2張千萬級(jí)別的表做一些OLAP切分操作,中間表的建立使用2種不同的方式。最后把中間表的數(shù)據(jù)按照要求取出,插入到結(jié)果表中
實(shí)驗(yàn)?zāi)康?測(cè)試臨時(shí)內(nèi)存表和臨時(shí)表的速度
1.中間表的建立使用Create temporary table type = heap 即 把中間表建立成臨時(shí)內(nèi)存表
2.中間表直接使用Create temporary table建立
實(shí)驗(yàn)結(jié)果:
臨時(shí)內(nèi)存表: 1小時(shí)
1 2008-09-25 11:03:48
1 2008-09-25 12:03:39
臨時(shí)表:1小時(shí)17分鐘
2 2008-09-25 12:25:28
2 2008-09-25 13:42:37
由此發(fā)現(xiàn)MEMORY比MyISAM快大概20%。
接著查找官方手冊(cè):
As indicated by the name, MEMORY tables are stored in memory. They use hash indexes by default, which makes them very fast, 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.
可以看出來MEMORY確實(shí)是very fast,and very useful for creating temporary tables .把臨時(shí)表和內(nèi)存表放在一起使用確實(shí)會(huì)快不少:create table tmp2(id int not null) engine memory;
內(nèi)存表的建立還有一些限制條件:
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.
在同一時(shí)間需要足夠的內(nèi)存.
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.為了釋放內(nèi)存,你應(yīng)該執(zhí)行DELETE FROM heap_table或DROP TABLE heap_table。
總結(jié)
以上是生活随笔為你收集整理的mysql 临时表 heap_mysql优化: 内存表和临时表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: action mutation 调用_V
- 下一篇: windows mysql is rea