You are here

MySQL Performance - eliminating ORDER BY function

MySQL Performance Blog - Wed, 17/10/2007 - 12:24pm

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:

PLAIN TEXT SQL:
  1. mysql> EXPLAIN SELECT * FROM tst WHERE i=5 AND date(d)=date(now()) ORDER BY date(d) \G
  2. *************************** 1. row ***************************
  3.            id: 1
  4.   select_type: SIMPLE
  5.         TABLE: tst
  6.          type: ref
  7. possible_keys: i
  8.           KEY: i
  9.       key_len: 5
  10.           ref: const
  11.          rows: 10
  12.         Extra: USING WHERE
  13. 1 row IN SET (0.00 sec)
  14.  
  15. mysql> EXPLAIN SELECT * FROM tst WHERE i=5 AND date(d)=date(now()) ORDER BY d \G
  16. *************************** 1. row ***************************
  17.            id: 1
  18.   select_type: SIMPLE
  19.         TABLE: tst
  20.          type: ref
  21. possible_keys: i
  22.           KEY: i
  23.       key_len: 5
  24.           ref: const
  25.          rows: 10
  26.         Extra: USING WHERE; USING filesort
  27. 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:

PLAIN TEXT SQL:
  1. mysql> EXPLAIN SELECT * FROM tst WHERE i=5 AND date(d)=date(now()) ORDER BY unix_timestamp(date(d)) \G
  2. *************************** 1. row ***************************
  3.            id: 1
  4.   select_type: SIMPLE
  5.         TABLE: tst
  6.          type: ref
  7. possible_keys: i
  8.           KEY: i
  9.       key_len: 5
  10.           ref: const
  11.          rows: 10
  12.         Extra: USING WHERE; USING filesort
  13. 1 row IN SET (0.00 sec)

Entry posted by peter | One comment

Add to: delicious | digg | reddit | netscape | Google Bookmarks