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.

由此可以看出來內存表會把表結構存放在磁盤上,把數據放在內存中
並做了以下實驗:
臨時表
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)

內存表
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)

可以看出來臨時表和內存表的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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章