测试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()

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。