"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 ?
-
How much time does it take, and how much time do you expect it to take? – Greg Hewgill Jul 15 '09 at 11:10
-
3wow so many answers and yet suggesting the same thing... – Umair Ahmed Jul 15 '09 at 11:19
-
its taking 3 to 4 seconds and that is too much i think – Jinesh Jul 15 '09 at 13:14
7 Answers
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.)

- 23,670
- 6
- 53
- 72
Adding appropriate indexes for the fields you're ordering by should do the trick.

- 168,566
- 31
- 210
- 223
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.

- 951,095
- 183
- 1,149
- 1,285
ALTER TABLE `tablename` ADD INDEX `indexname` (`columnname`);
Generally, indexname is the same as columnname.

- 11,062
- 7
- 33
- 35
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.

- 31,689
- 78
- 245
- 403
you may need to indexing the column on which you want to run order by . you can run the sql
'ALTER TABLE
yourTableNameADD INDEX
ColumnName (
ColumnName);
'
i had the same problem, and overcame by this procedure.

- 68
- 6