测试sql 语句执行时间

#-*-coding:utf-8-*-

import sys
import os
import time
import traceback
import MySQLdb as sqldb
from log import tracelog


query_list = [
"select count(*) from T_FieldInfo,T_FamTableDescInfo,T_DataEleDesc;",            
]

def getconn():
    host = ‘10.1.81.164‘
    user = ‘db_user‘
    passwd = ‘db_passwd‘
    dbname = ‘db_name‘
    return sqldb.connect(host,user,passwd,dbname)

def query_time(cursor,query):
    cursor.execute("flush hosts;")
    time.sleep(10)
   
    begintime = time.time()
    cursor.execute(query)
    endtime = time.time()
    spantime = endtime - begintime
    note = "sql:%s  spantime:%s" %(query,str(spantime))
    return note

def openmysqport():
    strcmd=r‘plink root@%s -pw %s "iptables -D INPUT -i bond1 -p tcp --dport 3306 -j DROP " ‘ %(‘10.1.81.164‘,‘root_passwd‘)
    os.system(strcmd) 
       

def main():
    tracelog.setLogFileName(r"./query_time_st.txt")
    tracelog.log("======sql test begin==========")
   
    try:
        conn = getconn()
        cursor = conn.cursor()
        for query in query_list:
            qnote = query_time(cursor,query)
            tracelog.log(qnote)
    except:
        expmsg = ‘‘.join(traceback.format_exception(*sys.exc_info()))
        tracelog.log(expmsg)
    finally:
        cursor.close()
        conn.close()
       
    tracelog.log("======sql test end==========")
   

if __name__ == ‘__main__‘:
    main()

测试sql 语句执行时间

上一篇:hiberante对象方式的投影操作与sql语句的投影方式(聚集函数的hibernate用法)


下一篇:Starting MySQL...The server quit without updating PID file [失败]lib/mysql/localhost.localdomain.pid).