#-*-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 [email protected]%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 ()
Test SQL statement Execution time