Every so often you need to perform sort results retrieved from MySQL when your WHERE clause goes beyound col=const values which would allow MySQL to still use second portion of the index for the order by. Ranges as well as IN lists make this optimization impossible, not even speaking about index merge optimization. Lets look at this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | CREATE TABLE `utest` ( `c1` int(10) unsigned NOT NULL, `c2` int(10) unsigned NOT NULL, `ord` int(10) unsigned NOT NULL, KEY `c1` (`c1`,`ord`), KEY `c2` (`c2`,`ord`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 mysql> explain select * from utest where c1=5 or c2=5 order by ord desc limit 10 \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: utest type: index_merge possible_keys: c1,c2 key: c1,c2 key_len: 4,4 ref: NULL rows: 162380 Extra: Using sort_union(c1,c2); Using where; Using filesort 1 row in set (0.00 sec) |
As you can see MySQL 5.1.21 uses sort_merge to access the rows but it can’t use it together with order by efficiently.
I’d say this is limitation as for this case you well could be retrieving sorted streams by the indexes and doing merge sort to get resulted rows in sorted order. Once this is implemented similar approach could be used for optimizing ORDER BY with IN.
This original query (in memory data) takes 1sec. Let’s see how classic pre MySQL 5.0 solution – using UNION instead of single query works in this case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | mysql> explain (select * from utest where c1=5) union (select * from utest where c2=5) order by ord desc limit 10 \G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: utest type: ref possible_keys: c1 key: c1 key_len: 4 ref: const rows: 108112 Extra: *************************** 2. row *************************** id: 2 select_type: UNION table: utest type: ref possible_keys: c2 key: c2 key_len: 4 ref: const rows: 54268 Extra: *************************** 3. row *************************** id: NULL select_type: UNION RESULT table: <union1,2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: Using filesort 3 rows in set (0.00 sec) |
The query looks scare but in fact in completes in 1.05 sec not significantly faster than sort index merge in this case.
As the query time implies MySQL is not smart enough in this case to “dive into” the union and add ORDER BY ORD LIMIT 10 to individual queries.
What if we do int manually ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | mysql> explain (select * from utest where c1=5 order by ord desc limit 10) union (select * from utest where c2=5 order by ord desc limit 10) order by ord desc limit 10 \G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: utest type: ref possible_keys: c1 key: c1 key_len: 4 ref: const rows: 108112 Extra: Using where *************************** 2. row *************************** id: 2 select_type: UNION table: utest type: ref possible_keys: c2 key: c2 key_len: 4 ref: const rows: 54268 Extra: Using where *************************** 3. row *************************** id: NULL select_type: UNION RESULT table: <union1,2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: Using filesort 3 rows in set (0.00 sec) |
As you can see explain does not really change (it does not show if index is used for finding rows or sorting as well) but query speed goes to less than
0.01 sec.
So it is great optimization which you need to do manuall for the time being.
What is also interesting is the fact MySQL is unable to handle even basic UNION with limit (without order by) optimally – in creates result set for the union fully and when only takes 10 rows from it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | mysql> explain (select * from utest where c1=5 ) union (select * from utest where c2=5 ) limit 10 \G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: utest type: ref possible_keys: c1 key: c1 key_len: 4 ref: const rows: 108112 Extra: *************************** 2. row *************************** id: 2 select_type: UNION table: utest type: ref possible_keys: c2 key: c2 key_len: 4 ref: const rows: 54268 Extra: *************************** 3. row *************************** id: NULL select_type: UNION RESULT table: <union1,2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: 3 rows in set (0.00 sec) |
Such query takes about 0.9 sec on the test data. Another possible optimizer improvement to do.
P.S This post is inspired by Does MySQL Optimize UNION with LIMIT clause topic on our MySQL Forums.
Peter,
Sorry, but you made a very small mistake …. It should be UNION ALL and not just UNION . As you know, UNION without attribute is UNION DISTINCT.
Yes, Sinisa I know
But this is closer to what A=5 or B=5 does. If you have UNION ALL rows which have A=5 and B=5 will be included twice in result set 🙂
testing on my notebook and utest with more than 1,000,000 rows, with mysql 5.0.45
(SELECT * FROM utest WHERE c1=5) union (SELECT * FROM utest WHERE c2=5) ORDER BY ord DESC LIMIT 10;
10 rows in set (0.31 sec)
(SELECT * FROM utest WHERE c1=5 ORDER BY ord DESC LIMIT 10) union (SELECT * FROM utest WHERE c2=5 ORDER BY ord DESC LIMIT 10) ORDER BY ord DESC LIMIT 10
10 rows in set (0.25 sec)
the speed is not so big differenet than you did
David,
Can you do EXPLAIN on your queries ?
0.25 sec is just very wrong time for such query as all what needs to be done in second query is fetching 10 rows by one index when 10 rows by another index and them sorting them.
Peter,
You are right about rows with A=5 and B=5 being included twice, but you can add … AND B != 5 .. etc… It would still be significantly faster then with UNION DISTINCT.
Peter,
Sorry I made a mistake. Finally the query
(SELECT * FROM utest WHERE c1=5 ORDER BY ord DESC LIMIT 10) union (SELECT * FROM utest WHERE c2=5 ORDER BY ord DESC LIMIT 10) ORDER BY ord DESC LIMIT 10,takes only 0.03
thanks a lot i was searching for one of my query, i would love to specially thanks to Devid. because i just wanted query which he has posted here.
many thanks.
I’m creating a shopping cart type system where users can add a bunch of products. It’s important that users can change the display order of products. This way they can showcase certain items on top.
All of this is stored in the database obviously.
There is a problem though, and it’s with the sort order storage in the database. For instance, if I have 5 products, I can set it like this:
01 A
10 B
20 C
30 D
40 E
The number is the “sort” field, and the letter is the “product” field. I’d then set an index for the “sort” field and the listings would be fast.
Now, suppose I wanted to re-order E to position number two (right now it’s position number 5). I could do this:
01 A
05 E
10 B
20 C
30 D
The problem here is, if I keep re-sorting, I’d run out of numbers in between and will have to re-order everything. This can be a potential nightmare if I have thousands of products.
Is there a better way of doing this?