optmizing ORDER BY in MySQL

A few days back we were trying to optimize a sql query that was using an ORDER BY clause. When we ran an EXPLAIN on the query, we saw that the query was Using filesort and Using temporary. Since this query was run quite frequently, there was a benefit in optimizing this.

I had been under the impression that if your query uses filesort and then has to use temp tables for sorting, you cannot do much to save the query. However, that is not the case. If you have an index on the column being used to do the order by , mysql will use that index for sorting. Of course, there are caveats to this, the most important one being

The key used to fetch the rows is not the same as the one used in the ORDER BY: SELECT * FROM t1 WHERE key2=constant ORDER BY key1;

Hence, if you have a query which requires to be sorted, and it is being run quite frequently, it makes sense to add an index on the column on which you are doing the sort.

Defined tags for this entry: ,



ILUG Bengaluru Meetup - Saturday 21st February 2009

Linux User Group, Bangalore have a meetup at the ThoughtWorks office on Saturday 21st February 2009, 4:30 PM onwards. The details of the meeting are here. There are 2 talks lined up- "Introduction to the Android Platform" and "Device Mapper - How it works and why you should be using it". And there will be a key signing party as well.

Go, check it out and help revive the LUG meetings at Bangalore.

Defined tags for this entry: ,

Un-minimizing apps in Mac

This tip saved a lot of pain.

If the application you're switching to has windows that are minimized, you can unminimize them by holding the option key, then letting go of the Cmd key.


7 things

Thanks to Lig for tagging me. 7 things you may or may not know about me:

  • I started a company, along with my friends, after I passed out of college. We just made one sale. Our project was made using mysql-3.x and VB. The implications here are - I have worked on VB, I know how to make mysql & VB work together and I know how to generate nice reports with the VB report generator.
  • I have worked in Goa. In my opinion, Goa is the best place in India that I have ever stayed in.
  • I used to own a Bullet 350 CC motorcycle. It is a great piece of machinery and in fact, it is the only vehicle which I DON'T consider to be a part of the "Get-me-from-point-A-to-B" class
  • spo0nman referred my name to Yahoo! and he gave me the boost of confidence when I was unsure if I am good enough to be even interviewed by them.
  • When I joined Yahoo!, I had to move from Delhi to Bangalore. However, the truck transporting my household stuff was burnt on its way to Bangalore. I had to start with a clean slate.
  • I had to clear many hurdles to get married. It was a constant 2 year struggle with my family, my wife's family and random circumstances. However, the end result was well worth the struggle.
  • I sometimes cry at movies and I find those sort of movies emotionally draining.
Defined tags for this entry:


Page 16 of 62, totaling 430 entries