MySQL query optimization - indexing
Well, in school we talked so much about tuning, I even took a class that devoted part of it to MySQL tuning, but I had yet to see a need for it until today. We have a site that wanted an area to each video page that pulled in content like Amazon.com. We titled the area "People who viewed this also viewed" and we were pulling in content that was seen by other users and highly rated. So, here's the sql I came up with:
SELECT path, COUNT(path) as count FROM accesslog WHERE uid IN (SELECT uid FROM accesslog WHERE path='*****' AND uid !=0) GROUP BY path ORDER BY count
With the asterisks being replaced with the path of the current page.
I learned a few things here. First off, I remembered from school that the COUNT() function is pretty taxing if you don't have an index on the column, and I also remembered that subqueries are pretty hard unless you have indexing. So, I checked the table for indexes, and it had one on the uid field, but it didn't have one on path. I figure with all the work I'm doing with path, it would be a good idea to put an index on one. So I used the MySql Administrator tool to open the table, analyze the indexes that were there, and add the one on the path field.
I'm getting ahead of myself. First off, I took an example query, and plugged in a path to one of the pages I was seeing this problem on. It took 17.2483 seconds (there's a lot of data in the accesslog table). Once I made the change to the index, I ran a similar query (one with a different path, so as to not pull from the mysql query cache), and it ran in .8188 seconds. That was truly amazing to me. Take it down by such a large amount just by adding a single index.