tags:

views:

157

answers:

6

"order by" in query is taking to much time in MySQL. SHOW PROFILES shows that the time is taken by the sorting process. Is there any setting/workaround that will decrease the sorting time ?

+5  A: 

Adding appropriate indexes for the fields you're ordering by should do the trick.

Philippe Leybaert
+1  A: 

You may be able to increase the speed of returning sorted results by adding an index on the column(s) that you want your results ordered by.

Greg Hewgill
+6  A: 

If you don't have an index on the field that you're ordering by, add one:

"In some cases, MySQL can use an index to satisfy an ORDER BY clause without doing any extra sorting."

Edit: (From the section on ORDER BY optimization in the MySQL documentation.)

Martin B
i am using the index ,but still MySQL is not using file sort instead
Jinesh
A: 

Create Index. If u don't have index do that.. it might be helpful

joe
+1  A: 
ALTER TABLE `tablename` ADD INDEX `indexname` (`columnname`);

Generally, indexname is the same as columnname.

Gav
A: 

Can you let me know the output of the following 2 commands: show create table tbl_name explain "your select query"

MySQL will not use index if it thinks that almost all the rows needs to be examined unless there is a covering index. Since only one index per table is used, try to order by the column that is part of that index if it being used at all.

shantanuo