I have this code below that needs to query multiple database tables.
import mysql.connector
import itertools, time
cnx = mysql.connector.connect(host="host", port=3306, user="user", passwd="pass", db="factory")
cursor = cnx.cursor()
cursor.execute("select distinct(batch) from batchdata where equipment = 'DUV001'")
batch = cursor.fetchall()
cursor.execute("select distinct(settingname) from runsettings where runnumber = 1 and equipment = 'DUV001'")
settings = cursor.fetchall()
start = time.time()
settings = list(itertools.chain(*settings))
batches = list(itertools.chain(*batch))
lstoflst = []
for batch in batches:
cursor.execute("select distinct(wafer) from batchdata where batch=%s and equipment = 'DUV001'",(batch,))
wafers = cursor.fetchall()
wafers = list(itertools.chain(*wafers))
for wafer in wafers:
waflist = {}
for i in settings:
cursor.execute("select float8value from runsettings where settingname = %s and wafer = %s",(i,wafer))
vals = cursor.fetchall()
waflist[i] = vals[0][0]
lstoflst.append(waflist)
print time.time() - start
I know there are a lot of things to improve like:
- Error handling
- Database connection
- prepared statements on the query
But I want to focus first on the performance of this code. It works fine, but its taking at least 5 to 10 seconds minimum before it can finish running the loop.
In what way can I improve the performance of the code above?
res = n*m
an optimization overres, i = 0,0; while i < m: res += n; i += 1
. Neither for joins, it's simply using the right tool. \$\endgroup\$ – 301_Moved_Permanently Nov 24 '15 at 11:13