《Mysql實例mysql優(yōu)化之內(nèi)存表與臨時表》要點:
本文介紹了Mysql實例mysql優(yōu)化之內(nèi)存表與臨時表,希望對您有用。如果有疑問,可以聯(lián)系我們。
MYSQL入門由于直接使用臨時表來創(chuàng)建中間表,其速度不如人意,因而就有了把臨時表建成內(nèi)存表的想法.
MYSQL入門但內(nèi)存表和臨時表的區(qū)別且并不熟悉,需要查找資料了.
一開始以為臨時表是創(chuàng)建后存在,當連接斷開時臨時表就會被刪除,即臨時表是存在于磁盤上的.而實際操作中發(fā)現(xiàn)臨時表創(chuàng)建后去目錄下查看發(fā)現(xiàn)并沒有發(fā)現(xiàn)對應的臨時表文件(未斷開鏈接).因而猜測臨時表的數(shù)據(jù)和結(jié)構(gòu)都是存放在內(nèi)存中,而不是在磁盤中.
MYSQL入門這樣一想內(nèi)存表不是也是存在在內(nèi)存中嗎,那么它與臨時表有什么區(qū)別?速度如何?
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.
MYSQL入門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入門由此可以看出來內(nèi)存表會把表結(jié)構(gòu)存放在磁盤上,把數(shù)據(jù)放在內(nèi)存中.
并做了以下實驗:
臨時表
?
MYSQL入門mysql> create temporary table tmp1(id int not null);
query ok, 0 rows affected (0.00 sec)
MYSQL入門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入門內(nèi)存表
?
MYSQL入門mysql> create table tmp2(id int not null) type=heap;
query ok, 0 rows affected (0.00 sec)
MYSQL入門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)
MYSQL入門可以看出來臨時表和內(nèi)存表的engine 不同,臨時表默認的是myisam,而內(nèi)存表是memory .去數(shù)據(jù)庫目錄查看,發(fā)現(xiàn)tmp2.frm而沒有tmp1表的任何文件.看來實際情況是符合官方解釋的.
MYSQL入門那么速度方面呢(即myisam和memory之間的區(qū)別)?
實驗開始:
實現(xiàn)手段:對基于2張千萬級別的表做一些olap切分操作,中間表的建立使用2種不同的方式.最后把中間表的數(shù)據(jù)依照要求取出,插入到結(jié)果表中
實驗目的;測試臨時內(nèi)存表和臨時表的速度
1.中間表的建立使用create temporary table type = heap 即 把中間表建立成臨時內(nèi)存表
2.中間表直接使用create temporary table建立
MYSQL入門實驗結(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
MYSQL入門由此發(fā)現(xiàn)memory比myisam快大概20%.
MYSQL入門接著查找官方手冊:
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.
MYSQL入門可以看出來memory確實是very fast,and very useful for creating temporary tables .把臨時表和內(nèi)存表放在一起使用確實會快不少:create table tmp2(id int not null) engine memory;
MYSQL入門內(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.
MYSQL入門為了釋放內(nèi)存,應該執(zhí)行:
delete from heap_table或drop table heap_table.
mysql 內(nèi)存表基礎(chǔ)知識
mysql 內(nèi)存表與臨時表有哪些區(qū)別
mysql創(chuàng)建內(nèi)存表辦法
有關(guān)MySQL內(nèi)存表的特性及使用介紹
mysql 內(nèi)存表在主從同步時的注意事項
《Mysql實例mysql優(yōu)化之內(nèi)存表與臨時表》是否對您有啟發(fā),歡迎查看更多與《Mysql實例mysql優(yōu)化之內(nèi)存表與臨時表》相關(guān)教程,學精學透。維易PHP學院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/8895.html