MySQL Tables and Tribulations
March 29th, 2008 | by Jez |Whilst working on a database query for part of my project, I had to join a few tables in a query. It was nothing out of the ordinary, however after some alterations and debugging later, I found out the query execution time took over 2 seconds! This was incredibly worrying, so I started performing some immense debugging and I couldn’t for the life of me figure out why this query should be executing to slowly.
The problem I later found out was hiding in plain site, I had completely over looked a table which was left in the select from part of the query which wasn’t called anywhere.
I was amazed afterwards that leaving a table in there by accident would cause such a massive slow down, the execution time went from 2.02s to 0.06s. However on reflection I assume MySQL is pulling all the info from the specified table and as it contained a lot of rows I imagine added a fairly large overhead on the database. My original query looked similar to:
-
SELECT table1.*, table2.image FROM table1, table2, table3, table4 WHERE table1.id = table2.table1id AND table1.id = table3.table1id AND ( (table3.value=‘value1′ AND table3.value2 = ‘value2′) OR (table3.value=‘genericvalue’))
As can be seen from this, table4 is not called anywhere in the statement, causing all sorts of havoc. So if your getting slow queries and it doesn’t make sense, be sure to check that you haven’t accidentally included an extra table!
Sorry, comments for this entry are closed at this time.