Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a MSSQL query execution time using python

can any one tell how to calculate a execution time of a MSSQL stored procedure query using python. I have a query like this

 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

i want to know the execution time of the query. I dont know how to do that. Pls help

Expected output:

 [(1, aa,vir,123/12, aaa@gmailcom,88898976),(2,bb,yuv,23/4, [email protected],2124314)]

 Time Taken To execute: 10s
like image 616
Nirmala Avatar asked Sep 17 '25 22:09

Nirmala


2 Answers

from time import time

# your code here

tic = time()
cursor.execute("select * from db.customer")
toc = time()
print toc - tic
like image 64
YXD Avatar answered Sep 19 '25 12:09

YXD


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)  )
like image 25
Brian Sanchez Avatar answered Sep 19 '25 12:09

Brian Sanchez