-- 建立索引 (在SQLyob下執(zhí)行)
ALTER TABLE tbl_heap ADD INDEX idx_name (name);
ALTER TABLE tbl_isam ADD INDEX idx_name (name);
Heap engine alter table add index used time: 2.078
MyISAM engine alter table add index used time: 13.516
Heap engine select 10000 times, 1000 record used time: 153.48922395706
MyISAM engine select 10000 times, 1000 record used time: 239.86818814278
PS: 不合適的索引還不如不要。。。-_-#
[SQLyog下進(jìn)行count操作]
Heap engine select count used time: 4.53
MyISAM engine select count used time: 3.28
[SQLyob下進(jìn)行select操作]
速度都很快,都在1秒一下,不論是1000條記錄,還是200000條記錄,個(gè)人猜測可能不準(zhǔn)確
【更新操作】
[更新所有name=heiyeluren的記錄為heiyeluren2,在SQLyog下執(zhí)行]
Heap engine update used time: 2.500
MyISAM engine update used time: 16.000
【刪除操作】
[刪除所有name=heiyeluren2的記錄,在SQLyog下執(zhí)行]
Heap engine delete used time: 51.172
MyISAM engine delte used time: 5.578
【總結(jié)】
Heap在插入、查詢、更新操作上明顯要比MyISAM快,但是刪除操作稍微比較慢,可能跟它在內(nèi)存中的存儲結(jié)構(gòu)有關(guān)系,所以我們完全可以把Heap作為我們一個(gè)MyISAM表的一個(gè)備份,比如可以保存一些實(shí)時(shí)性要求比較高的數(shù)據(jù),比如點(diǎn)擊量、評論量、固定的用戶信息等等,因?yàn)檫@些信息普遍是插入和查詢操作。
當(dāng)然了,Heap也沒有比MyISAM快太多,這樣說明我的MyISAM速度還是非?斓模砸话闱闆r下,MyISAM能夠滿足大部分的應(yīng)用了,如果數(shù)據(jù)太多的情況,可以考慮把部分常用的數(shù)據(jù)保存到Heap表中,同時(shí)也可以結(jié)合Memcache等緩存工具來幫助緩存數(shù)據(jù)。
另外,關(guān)于InnoDB和MyISAM的性能測試,可以參考我之前的測試數(shù)據(jù):
http://blog.csdn.net/heiyeshuwu/archive/2007/04/10/1559640.aspx
【測試代碼】
<?php
/**
* function lib
*/
function conn(){
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$conn = mysql_connect($host, $user, $pass);
if (!$conn || !is_resource($conn)){
die("Connect to mysql failed: ". mysql_error());
}
mysql_select_db($db);
return $conn;
}
function query($conn, $sql){
$res = mysql_query($sql, $conn);
if (!$res){
echo mysql_error()." ";
return false;
}
return true;
}
function get_data($conn, $sql){
$res = mysql_query($sql, $conn);
if (!$res){
echo mysql_error()." ";
return false;
}
$result = array();
while($row = mysql_fetch_array($res)){
$result[] = $row;
}
return $result;
}
function get_time()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* Insert test
*/
$conn = conn();
$count = 200000;
//Heap engine
$s1 = get_time();
for($i=0; $i<$count; $i++){
query($conn, "insert into tbl_heap set name = 'heiyeluren', email='heiyeluren@abc.com', summary='This message is summary'");
}
$e1 = get_time();
echo "Heap engine insert $count record used time: ". ($e1-$s1) ." ";
//MyISAM engine
$s2 = get_time();
for($i=0; $i<$count; $i++){
query($conn, "insert into tbl_isam set name = 'heiyeluren', email='heiyeluren@abc.com', summary='This message is summary'");
}
$e2 = get_time();
echo "MyISAM engine insert $count record used time: ". ($e2-$s2) ." ";
/**
* Select test
*/
$count = 1000;
$records = 10000;
$s1 = get_time();
for($i=0; $i<$count; $i++){
$res = get_data($conn, "select * from tbl_heap limit $records");
unset($res);
}