I'm loop querying a few date ranges:
con = MySQLdb.connect(host='xxx.com', port=3306,user='user', passwd='pass', db='db')
intervals = 10
precision = 1
dateChunks = list()
for i in range(0,intervals,precision):
results = load_data_window(i,precision)
dateChunks.append(results)
def load_data_window(start,precision):
length = start + precision # <-- time period
limit = 20000
cur = con.cursor()
sql = "SELECT data FROM table WHERE date < DATE_SUB(NOW(), INTERVAL %s HOUR) AND date > DATE_SUB(NOW(), INTERVAL %s HOUR)"
cur.execute(sql,(start,length))
results = cur.fetchall()
It works lightning fast the first few loops, sometimes even all of them, but bogs significantly from time to time. There are other actions happening on the database from different places... Is there something I can do to ensure my query has priority? Something like a transaction?
######### EDITI did notice, if I move the con = MSQLdb... inside of the load_data_window function, I get really fast results and then bogging, whereas if I keep the con = MSQLdb... outside it is consistently slower...