While building applications for high traffic sites, making sure your queries are optimized is a very important step.. Even a single bad query can lock up your database and bring down the entire server
Here are a couple of ways to check for such Slow queries.
1) If you have Graphical interface available on your server then you can install MySQL Administrator
After installing Launch MySQL Administrator. Select Startup Variables from the Left Pane. Go to the Log Files Tab and Activate Slow Queries Log . Once its enabled all the queries taking more than 10 seconds would get logged. Obviously you can change that value to whatever you thing is slow for you.
To view this log
Select Server Logs from the left pane and then Go to the Slow Query Log tab which should give you a real time view of the ‘bad’ queries that are slowing down your server.
However in most cases you would be remotely managing a Linux server and SSH would be your only way to get in.
If that’s the case, then the nicest tool that I found was mytop a really sweet console based tool that gives you a list of the slow queries.
There were talks of mytop being available as an RPM so if you are lucky, a
yum install mytop
or
apt-get install mytop
should work for you.. However when I last tried, it didnt work for me so I had to do it the hard way
Luanch Putty or any other tool that you use to SSH into your server
and starting firing away at these commands
wget http://jeremy.zawodny.com/mysql/mytop/mytop-1.6.tar.gz
tar -zxvf mytop-1.6.tar.gz
cd mytop-1.6
perl Makefile.PL
make
make test
make install
If everything goes fine.. type in mytop to launch the application.
Now some of you like ‘moi’ might get the error message while trying to launch mytop
Error in option spec: “long|!”
To get rid of this we’ll need to comment out a line …so on the console type in
pico /usr/bin/mytop or gedit /usr/bin/mytop
Search for a line that says “”long|!” => \$config{long_nums}” and comment it out.. ( I have no idea what that line does anyways)
After you’ve done that save the file and relaunch mytop and hopefully you should see a constantly updating screen of slow queries.
Hitting q should bring you back to command prompt.
So now you are set to fix up all those queries that are messing up your server.