mysql優化: 內存表和臨時表 由于直接使用臨時表來創建中間表,其速度不如人意,因而就有了把臨時表建成內存表的想法。但內存表和臨時表的區別且并不熟悉,需要查找資料了。 ? 一開始以為臨時表是創建后存在,當連接斷開時臨時表就會被刪除,即臨時表是存在于
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.?
由此可以看出來內存表會把表結構存放在磁盤上,把數據放在內存中。?
并做了以下實驗:?
臨時表?
內存表?
可以看出來臨時表和內存表的ENGINE 不同,臨時表默認的是MyISAM,而內存表是MEMORY .去數據庫目錄查看,發現tmp2.frm而沒有tmp1表的任何文件。看來實際情況是符合官方解釋的。?
那么速度方面呢(即MyISAM和MEMORY之間的區別)??
實驗開始:?
實現手段:對基于2張千萬級別的表做一些OLAP切分操作,中間表的建立使用2種不同的方式。最后把中間表的數據按照要求取出,插入到結果表中?
實驗目的;測試臨時內存表和臨時表的速度?
1.中間表的建立使用Create temporary table type = heap 即 把中間表建立成臨時內存表?
2.中間表直接使用Create temporary table建立?
實驗結果:?
臨時內存表: 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?
由此發現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 .把臨時表和內存表放在一起使用確實會快不少:create table tmp2(id int not null) engine memory;?
內存表的建立還有一些限制條件:?
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。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com