Chris Nizzardini

Salt Lake City, Utah Developer / Human / Blogger

MySQL Optimization Tip: Checking for Differences in Queries.

I’ve been going through the MySQL slow query log at my new job to optimize our applications performance. It’s a bit more scary making changes when you’re the “new guy” because there are a lot of “gotchas” you haven’t quite figured out in the new platform. Well one of the queries that I wanted to optimize lead me to get rid of some needless LEFT JOINS and trim down the amount of fields returned in the result set. Aside from testing the application as a whole after making my modification I needed a fast way to verify the exact same data was being returned for all 50,000 rows. One option was to append each field to a string and then convert that into an MD5 hash for each variation of the query. That seemed half assed too me so I decided to export the results to a CSV file then run a diff against the two from the linux shell.

STEP 1 This is really easy to do. The diff command will tell you exactly where the differences are in the files giving you somewhere to start debugging. In PHPMyAdmin take your current production stable query, run it, click the export tab, select CSV, and save it to a file. Do the same for your shiny new optimized query.

PHPMyAdmin Export to CSV file

STEP 2 Now that you have both files saved to your file system. Run the DIFF command:

Linux Shell Diff Command

If there are NO differences the command will return NO output. If there are any differences you would get some output like this:

Linux Shell Diff Command

I hope this helps someone out and if there is a better way to do this please leave a comment.

Chris Nizzardini has been developing web applications since 2006. He lives and works in beautiful Salt Lake City, Utah. If you’re interested in hiring me for contract work please visit IO Spring LLC.

Twitter Google+ 

, ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>