如何使用python计算MSSQL查询执行时间

时间:2013-11-25 13:22:11

标签: python sql sql-server

任何人都可以告诉如何使用python计算MSSQL存储过程查询的执行时间。我有这样的查询

 import pyodbc

 import timer

 DSN ='DRIVER=FreeTDS;SERVER=255.8.12.34;PORT=1433;DATABASE=CustomerFile;UID=Cust;
       PWD=Cust;TDS_Version=8.0;'

 cnxn =pyodbc.connect(DSN)

 cursor = cnxn.cursor()

 cursor.execute("select * from db.customer")

 d = cursor.fetchall()

 print d

我想知道查询的执行时间。我不知道该怎么做。请帮忙

预期产出:

 [(1, aa,vir,123/12, aaa@gmailcom,88898976),(2,bb,yuv,23/4, bbb@gmail.com,2124314)]

 Time Taken To execute: 10s

2 个答案:

答案 0 :(得分:4)

from time import time

# your code here

tic = time()
cursor.execute("select * from db.customer")
toc = time()
print toc - tic

答案 1 :(得分:1)

python

import datetime

init_time          = datetime.datetime.now()

cursor.execute("select * from db.customer" )

end_time           = datetime.datetime.now()
exec_time          =  end_time - init_time

print ( 'exec_time  = {} seconds '.format( exec_time.seconds)  )