| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ORDER BY
In some cases MySQL can uses index to satisfy an ORDER BY or
GROUP BY request without doing any extra sorting.
The index can also be used even if the ORDER BY doesn't match the
index exactly, as long as all the unused index parts and all the extra
are ORDER BY columns are constants in the WHERE
clause. The following queries will use the index to resolve the
ORDER BY / GROUP BY part:
SELECT * FROM t1 ORDER BY key_part1,key_part2,... SELECT * FROM t1 WHERE key_part1=constant ORDER BY key_part2 SELECT * FROM t1 WHERE key_part1=constant GROUP BY key_part2 SELECT * FROM t1 ORDER BY key_part1 DESC,key_part2 DESC SELECT * FROM t1 WHERE key_part1=1 ORDER BY key_part1 DESC,key_part2 DESC |
Some cases where MySQL can not use indexes to resolve the ORDER
BY: (Note that MySQL will still use indexes to find the rows that
matches the WHERE clause):
ORDER BY on different keys:
SELECT * FROM t1 ORDER BY key1,key2
ORDER BY using non-consecutive key parts.
SELECT * FROM t1 WHERE key2=constant ORDER BY key_part2
ASC and DESC.
SELECT * FROM t1 ORDER BY key_part1 DESC,key_part2 ASC
ORDER BY:
SELECT * FROM t1 WHERE key2=constant ORDER BY key1
ORDER
BY on are not all from the first not-const table that is used to
retrieve rows (This is the first table in the EXPLAIN output which
doesn't use a const row fetch method).
ORDER BY and GROUP BY expressions.
HASH index in HEAP tables).
In the cases where MySQL have to sort the result, it uses the following algorithm:
WHERE clause are skipped.
sort_buffer).
MERGEBUFF (7) regions to one block in
another temporary file. Repeat until all blocks from the first file
are in the second file.
MERGEBUFF2 (15)
blocks left.
read_rnd_buffer_size) .
You can with EXPLAIN SELECT ... ORDER BY check if MySQL can use
indexes to resolve the query. If you get Using filesort in the
extra column, then MySQL can't use indexes to resolve the
ORDER BY. See section EXPLAIN.
If you want to have a higher ORDER BY speed, you should first
see if you can get MySQL to use indexes instead of having to do an extra
sorting phase. If this is not possible, then you can do:
sort_buffer_size variable.
read_rnd_buffer_size variable.
tmpdir to point to a dedicated disk with lots of empty space.
If you use MySQL 4.1 or later you can spread load between
several physical disks by setting tmpdir to a list of paths
separated by colon : (semicolon ; on Windows). They
will be used in round-robin fashion.
Note: These paths should end up on different physical disks,
not different partitions of the same disk.
By default, MySQL sorts all GROUP BY x,y[,...] queries as if you
specified ORDER BY x,y[,...] in the query as well. If you include the
ORDER BY clause explicitly, MySQL optimizes it away without any speed
penalty, though the sorting still occurs.
If a query includes GROUP BY but you want to avoid the overhead
of sorting the result, you can supress sorting by specifying
ORDER BY NULL:
INSERT INTO foo SELECT a,COUNT(*) FROM bar GROUP BY a ORDER BY NULL; |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |