MySQL Forums
Forum List  »  Optimizer & Parser

Re: Query execution plan
Posted by: Chris Slominski
Date: May 09, 2012 11:08AM

A modified query seems to solve the problem. In this example 3:42.11 goes to 0.00 using what would appear to be a significantly more complex query.

mysql> select * from table_38740 where time <= 5706114878826086400 order by time desc limit 1;
+---------------------+------+--------+
| time | code | val1 |
+---------------------+------+--------+
| 5706114875966008424 | 0 | -179.1 |
+---------------------+------+--------+
1 row in set (3 min 42.11 sec)

mysql> explain select * from table_38740 where time <= 5706114878826086400 order by time desc limit 1;
+----+-------------+-------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | table_38740 | index | PRIMARY | PRIMARY | 8 | NULL | 1 | Using where |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

--------------- Better ------------------

mysql> select * from table_38740 where time = (select max(time) from table_38740 where time <= 5706114878826086400);
+---------------------+------+--------+
| time | code | val1 |
+---------------------+------+--------+
| 5706114875966008424 | 0 | -179.1 |
+---------------------+------+--------+
1 row in set (0.00 sec)

mysql> explain select * from table_38740 where time = (select max(time) from table_38740 where time <= 5706114878826086400);
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+------------------------------+
| 1 | PRIMARY | table_38740 | const | PRIMARY | PRIMARY | 8 | const | 1 | |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+------------------------------+
2 rows in set (0.00 sec)

Options: ReplyQuote


Subject
Views
Written By
Posted
2984
May 08, 2012 12:38PM
Re: Query execution plan
1225
May 09, 2012 11:08AM
1292
May 09, 2012 09:35PM
1436
May 10, 2012 08:29AM
1379
May 11, 2012 07:16PM
1447
May 14, 2012 05:27AM
1204
May 15, 2012 07:22PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.