title: MySQL queries are fast when run directly but really slow when run as stored proc
body: <p>I've been trying to figure out what's wrong with a set of queries I've got and I'm just confused at this point.</p>
  
  <p>It's supposed to be in a stored procedure which gets called by a GUI application.</p>
  
  <p>There's only one "tiny" problem, it's first a simple <code>UPDATE</code>, then an <code>INSERT</code> using a <code>SELECT</code> with a subselect and finally another <code>UPDATE</code>. Running these queries together by hand I get a total execution time of 0.057s, not too shabby.</p>
  
  <p>Now, I try creating a stored procedure with these queries in it and five input variables, I run this procedure and on the first attempt it took 47.096s with subsequent calls to it showing similar execution times (35 to 50s). Running the individual queries from the MySQL Workbench still show execution times of less than 0.1s</p>
  
  <p>There really isn't anything fancy about these queries, so why is the stored procedure taking an eternity to execute while the queries by themselves only take a fraction of a second? Is there some kind of MySQL peculiarity that I'm missing here?</p>
  
  <p><strong>Additional testing results:</strong></p>
  
  <p>It seems that if I run the queries in MySQL Workbench but use variables instead of just putting the values of the variables in the queries it runs just as slow as the stored procedure. So I tried changing the stored procedure to just use static values instead of variables and suddenly it ran blazingly fast. Apparently for some reason using a variable makes it run extremely slow (for example, the first <code>UPDATE</code> query goes from taking approximately 0.98s with three variables to 0.04-0.05s when I use the values of variables directly in the query, regardless of if it's in the stored procedure or running the query directly).</p>
  
  <p>So, the problem isn't the stored procedure, it's something related to my use of variables (which is unavoidable).</p>