MySQL两种表存储结构MyISAM和InnoDB的性能比较测试
转载 http://www.jb51.net/article/5620.htm
测试环境:Redhat Linux9,4CPU,内存2G,MySQL版本为4.1.6-gamma-standard
测试程序:Python+Python-MySQL模块。
测试方案:
1、MyISAM格式分别测试,事务和不用事务两种情况:
2、InnoDB格式分别测试AutoCommit=1(不用begin transaction和用begin transaction模式),
AutoCommit=0 (不用begin transaction和用begin transaction模式)四种情况。
1、MyISAM不用事务表:
CREATE TABLE `MyISAM_NT` (
`TableId` int(11) NOT NULL default ‘0‘,
`TableString` varchar(21) NOT NULL default ‘‘
) ENGINE=MyISAM;
CREATE TABLE `MyISAM_TS` (
`TableId` int(11) NOT NULL default ‘0‘,
`TableString` varchar(21) NOT NULL default ‘‘
) ENGINE=MyISAM;
CREATE TABLE `INNODB_NA_NB` (
`TableId` int(11) NOT NULL default ‘0‘,
`TableString` varchar(21) NOT NULL default ‘‘
) ENGINE=InnoDB;
CREATE TABLE `INNODB_NA_BE` (
`TableId` int(11) NOT NULL default ‘0‘,
`TableString` varchar(21) NOT NULL default ‘‘
) ENGINE=InnoDB;
CREATE TABLE `INNODB_AU_NB` (
`TableId` int(11) NOT NULL default ‘0‘,
`TableString` varchar(21) NOT NULL default ‘‘
) ENGINE=InnoDB;
CREATE TABLE `INNODB_AU_BE` (
`TableId` int(11) NOT NULL default ‘0‘,
`TableString` varchar(21) NOT NULL default ‘‘
) ENGINE=InnoDB;
‘‘‘
MyISAM,InnoDB性能比较
作者:空心菜(Invalid)
时间:2004-10-22
‘‘‘
import MySQLdb
import sys
import os
import string
import time
("MyISAM_TS",None,1),
("INNODB_NA_NB",0,0),
("INNODB_NA_BE",0,1),
("INNODB_AU_NB",1,0),
("INNODB_AU_BE",1,1)
]
c.execute("BEGIN;")
return
print "ExecSQL:COMMIT;"
c.execute("COMMIT;")
return
print "ExecSQL:Set AUTOCOMMIT = "+str(flag)
c.execute("Set AUTOCOMMIT = "+str(flag))
return
def getcount(table):
#print "ExecSQL:select count(*) from "+table
c.execute("select count(*) from "+table)
return c.fetchall()[0][0]
def AddTable (Table,TableId,TableString):
try:
c.execute(sql)
except MySQLdb.OperationalError,error:
print "AddTable Error:",error
return -1;
return c.rowcount
def main():
print ‘Usage:‘,argv[0],‘ TableId TestCount \n‘
sys.exit(1)
global c #mysql访问cursor
db_host = "localhost"
db_name = "demo"
db_user = "root"
db_user_passwd = ""
print "Config:[%s %s/%s %s] DB\n"%(db_host,db_user,db_user_passwd,db_name)
if len(argv) > 2:
tableid = argv[1]
testcount = int(argv[2]) #
#每次操作前都重写建立数据库连接
try:
mdb = MySQLdb.connect(db_host, db_user, db_user_passwd, db_name)
except MySQLDb.OperationalError,error:
print "Connect Mysql[%s %s/%s %s] DB Error:"%(db_host,db_user,db_user_passwd,db_name),error,"\n"
sys.exit(1)
else:
c = mdb.cursor()
table,autocommit,trans = test
starttime = time.time()
print table," ",time.strftime("%y-%m-%d %H:%M:%S",time.localtime())
AutoCommit(autocommit)
if trans == 1:
BeginTrans()
tablestring = "%020d"%i
if (AddTable(table,tableid,tablestring)<1):
print "AddTable Error",tablestring
if trans == 1:
Commit()
print time.strftime("%y-%m-%d %H:%M:%S",time.localtime())
endtime = time.time()
usedtime = endtime-starttime
c.close()
mdb.close()
if __name__ == ‘__main__‘:
main()
测试结果如下:
Config:[localhost root/ demo] DB
04-10-22 16:33:26
MyISAM_NT count: 10000 used time: 2.1132440567
MyISAM_TS 04-10-22 16:33:26
ExecSQL:BEGIN;
ExecSQL:COMMIT;
04-10-22 16:33:29
MyISAM_TS count: 10000 used time: 2.65475201607
INNODB_NA_NB 04-10-22 16:33:29
ExecSQL:Set AUTOCOMMIT = 0
04-10-22 16:33:31
INNODB_NA_NB count: 10000 used time: 2.51947999001
INNODB_NA_BE 04-10-22 16:33:31
ExecSQL:Set AUTOCOMMIT = 0
ExecSQL:BEGIN;
ExecSQL:COMMIT;
04-10-22 16:33:35
INNODB_NA_BE count: 10000 used time: 3.85625100136
INNODB_AU_NB 04-10-22 16:33:35
ExecSQL:Set AUTOCOMMIT = 1
04-10-22 16:34:19
INNODB_AU_NB count: 10000 used time: 43.7153041363
INNODB_AU_BE 04-10-22 16:34:19
ExecSQL:Set AUTOCOMMIT = 1
ExecSQL:BEGIN;
ExecSQL:COMMIT;
04-10-22 16:34:22
INNODB_AU_BE count: 10000 used time: 3.14328193665
结论:
由此得知影响速度的主要原因是AUTOCOMMIT默认设置是打开的,
我当时的程序没有显式调用BEGIN;开始事务,导致每插入一条都自动Commit,严重影响了速度。
算来也是个低级错误!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。