MySQL两种表存储结构MyISAM和InnoDB的性能比较测试

2019-01-03 13:42:10王冬梅
def BeginTrans():
    print "ExecSQL:BEGIN;"
    c.execute("BEGIN;")

    return
def Commit():

    print "ExecSQL:COMMIT;"
    c.execute("COMMIT;")
    return
def AutoCommit(flag):

    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):
    sql = "INSERT INTO "+Table+"(TableId, TableString) VALUES( "+ TableId+ ",'" + TableString +"')"
    try:
        c.execute(sql)
    except MySQLdb.OperationalError,error:
        print "AddTable Error:",error
        return -1;
    return c.rowcount

def main():
    argv = sys.argv
    if len(argv) < 2:
        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] DBn"%(db_host,db_user,db_user_passwd,db_name)

    if len(argv) > 2:
        tableid = argv[1]
        testcount = int(argv[2])   #
    for test in testtables:
        #每次操作前都重写建立数据库连接   
        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())