mysql優(yōu)化: 內(nèi)存表和臨時表 由于直接使用臨時表來創(chuàng)建中間表,其速度不如人意,因而就有了把臨時表建成內(nèi)存表的想法。但內(nèi)存表和臨時表的區(qū)別且并不熟悉,需要查找資料了。 ? 一開始以為臨時表是創(chuàng)建后存在,當(dāng)連接斷開時臨時表就會被刪除,即臨時表是存在于
mysql優(yōu)化: 內(nèi)存表和臨時表由于直接使用臨時表來創(chuàng)建中間表,其速度不如人意,因而就有了把臨時表建成內(nèi)存表的想法。但內(nèi)存表和臨時表的區(qū)別且并不熟悉,需要查找資料了。?
一開始以為臨時表是創(chuàng)建后存在,當(dāng)連接斷開時臨時表就會被刪除,即臨時表是存在于磁盤上的。而實際操作中發(fā)現(xiàn)臨時表創(chuàng)建后去目錄下查看發(fā)現(xiàn)并沒有發(fā)現(xiàn)對應(yīng)的臨時表文件(未斷開鏈接).因而猜測臨時表的數(shù)據(jù)和結(jié)構(gòu)都是存放在內(nèi)存中,而不是在磁盤中.?
這樣一想內(nèi)存表不是也是存在在內(nèi)存中嗎,那么他和臨時表有什么區(qū)別?他們的速度是什么樣子??
查找了官方手冊有以下的一些解釋:?
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)存表會把表結(jié)構(gòu)存放在磁盤上,把數(shù)據(jù)放在內(nèi)存中。?
并做了以下實驗:?
臨時表?
內(nèi)存表?
可以看出來臨時表和內(nèi)存表的ENGINE 不同,臨時表默認的是MyISAM,而內(nèi)存表是MEMORY .去數(shù)據(jù)庫目錄查看,發(fā)現(xiàn)tmp2.frm而沒有tmp1表的任何文件。看來實際情況是符合官方解釋的。?
那么速度方面呢(即MyISAM和MEMORY之間的區(qū)別)??
實驗開始:?
實現(xiàn)手段:對基于2張千萬級別的表做一些OLAP切分操作,中間表的建立使用2種不同的方式。最后把中間表的數(shù)據(jù)按照要求取出,插入到結(jié)果表中?
實驗?zāi)康?測試臨時內(nèi)存表和臨時表的速度?
1.中間表的建立使用Create temporary table type = heap 即 把中間表建立成臨時內(nèi)存表?
2.中間表直接使用Create temporary table建立?
實驗結(jié)果:?
臨時內(nèi)存表: 1小時?
1 2008-09-25 11:03:48?
1 2008-09-25 12:03:39?
臨時表:1小時17分鐘?
2 2008-09-25 12:25:28?
2 2008-09-25 13:42:37?
由此發(fā)現(xiàn)MEMORY比MyISAM快大概20%。?
接著查找官方手冊:?
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確實是very fast,and very useful for creating temporary tables .把臨時表和內(nèi)存表放在一起使用確實會快不少: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.?
在同一時間需要足夠的內(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。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com