Why does sleeping before a SQLAlchemy / MySQL UPDATE speed up my code a lot?

Viewed 45

My code runs hundreds of SQL statements (INSERTs, UPDATEs, and SELECTs) before it comes to the following block of code...

# time.sleep(0.25)  # TODO: Sleeping somehow speeds up the following query enormously
common.qry_engine.execute(sql)

If I comment out the sleep, as above, the code takes over a minute to run. If I leave the sleep in, however, it runs in well under a second. sql is as follows...

UPDATE `f04778f46d35db04105603117bd346fe`.`t_f2e92eee9d80bd6f5b9dc6999c3d84de` new
JOIN (SELECT NULL AS `Application Id`, NULL AS `First Name`, NULL AS `Last Name`, 
      NULL AS `Email`, 
      f04778f46d35db04105603117bd346fe.t_f2e92eee9d80bd6f5b9dc6999c3d84de.master_key 
      FROM f04778f46d35db04105603117bd346fe.t_f2e92eee9d80bd6f5b9dc6999c3d84de) gs
ON (new.master_key is NULL and gs.master_key IS NOT NULL AND
   ((`gs`.`Application Id` = `new`.`ApplicationId`)
OR ((`gs`.`First Name` = `new`.`FirstName`)
AND (`gs`.`Last Name` = `new`.`LastName`))))
SET new.master_key = gs.master_key;

FWIW, in this case, the UPDATE does not actually change any rows, which is correct.

What could be causing this speed issue? The sleep in my Python code is just a kluge and I'd like to remedy the situation because I don't know where else this could unexpectedly arise. Should I be looking at some setting in SQLAlchemy or in MySQL?

Appreciate any ideas. Thanks!

0 Answers
Related