MySQL Performance - eliminating ORDER BY function
One of the first rules you would learn about MySQL Performance Optimization is to avoid using functions when comparing constants or order by. Ie use indexed_col=N is good. function(indexed_col)=N is bad because MySQL Typically will be unable to use index on the column even if function is very simple such as arithmetic operation. Same can apply to order by, if you would like that to use the index for sorting. There are however some interesting exception.
Compare those two queries for example. If you look only at ORDER BY clause you would see first query which sorts by function is able to avoid order by while second which uses direct column value needs to do the filesort:
-
mysql> EXPLAIN SELECT * FROM tst WHERE i=5 AND date(d)=date(now()) ORDER BY date(d) \G
-
*************************** 1. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: tst
-
type: ref
-
possible_keys: i
-
KEY: i
-
key_len: 5
-
ref: const
-
rows: 10
-
Extra: USING WHERE
-
1 row IN SET (0.00 sec)
-
-
mysql> EXPLAIN SELECT * FROM tst WHERE i=5 AND date(d)=date(now()) ORDER BY d \G
-
*************************** 1. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: tst
-
type: ref
-
possible_keys: i
-
KEY: i
-
key_len: 5
-
ref: const
-
rows: 10
-
Extra: USING WHERE; USING filesort
-
1 row IN SET (0.00 sec)
If you take a closer look to WHERE clause you will find the reason - date(d) equals to date(now()) which is constant which means we're sorting by constant and so sort phase can be skipped all together.
Note in this case MySQL Optimizer is rather smart and is able to do this even if we have function in ORDER BY and exactly the same function is equals to constant by WHERE clause. If course it works for direct constants as well.
However if functions are different MySQL is not able to do this optimization even in cases when this would be possible:
-
mysql> EXPLAIN SELECT * FROM tst WHERE i=5 AND date(d)=date(now()) ORDER BY unix_timestamp(date(d)) \G
-
*************************** 1. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: tst
-
type: ref
-
possible_keys: i
-
KEY: i
-
key_len: 5
-
ref: const
-
rows: 10
-
Extra: USING WHERE; USING filesort
-
1 row IN SET (0.00 sec)
5 Comments











del.icio.us
digg
So the lesson to learn is if you always apply a specific function on a column, you might as well apply it before the actual insert so that you can leverage the index properly. Or denormalize ..
Comment :: October 17, 2007 @ 6:04 am
Slightly off-topic: talking about order by … trying to find solution for a long time to optimize queries with “order by field” like this:
EXPLAIN SELECT id, name
FROM table_name
WHERE id IN ( 222839, 299872, 301535 )
ORDER BY FIELD( id, 222839, 299872, 301535 )
FROM EXPLAIN:
—————————————
select_type SIMPLE
table table_name [innodb]
type range
possible_keys PRIMARY
key PRIMARY
key_len 4
ref NULL
rows 3
Extra Using where; Using filesort
Am I plain stupid or is it a bug (in the optimizer) in MySQL ?
Comment :: November 1, 2007 @ 11:40 am
Sorry, something’s missing … the problem is the filesort, of course
Comment :: November 1, 2007 @ 11:43 am
Why are you surprised you’re sorting by FIELD function - of course MySQL can’t use Index to do such sort.
Comment :: November 1, 2007 @ 3:56 pm
[...] about MySQL performance optimization. He and his partners of Percona write about topics like eliminating ORDER BY function or Be careful when joining on CONCAT. Peter also held presentations at all Mysql Conferences. In [...]
Pingback :: November 18, 2007 @ 11:29 am