I use MySQL 5.6.19-log (according to select version()
).
I have a InnoDB table something like this:
CREATE TABLE `mytable` (
`foo_id` bigint(20) NOT NULL,
`bar_id` bigint(20) NOT NULL,
`baz_id` bigint(20) NOT NULL,
PRIMARY KEY (`foo_id`,`bar_id`,`baz_id`)
)
This table works well with the following query:
select
foo_id,
min(bar_id)-1
from
mytable
where
foo_id IN (
1000,2000
)
group by
foo_id;
+----+-------------+----------------------+-------+------------------------+---------+---------+------+-------+---------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+-------+------------------------+---------+---------+------+-------+---------------------------------------+
| 1 | SIMPLE | mytable | range | PRIMARY,bar_id_idx | PRIMARY | 8 | NULL | 58245 | Using where; Using index for group-by |
+----+-------------+----------------------+-------+------------------------+---------+---------+------+-------+---------------------------------------+
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | 0.00036575 | select foo_id, min(bar_id)-1 from mytable where foo_id IN (1000,2000) group by foo_id |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
But, when the number of foo_id
in the where clause is only one, the query becomes quite slow, as follows:
select
foo_id,
min(bar_id)-1
from
mytable
where
foo_id = 1000
group by
foo_id;
+----+-------------+----------------------+------+------------------------+---------+---------+-------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+------------------------+---------+---------+-------+--------+-------------+
| 1 | SIMPLE | mytable | ref | PRIMARY,bar_id_idx | PRIMARY | 8 | const | 873664 | Using index |
+----+-------------+----------------------+------+------------------------+---------+---------+-------+--------+-------------+
+----------+------------+-----------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------------------------------------------------------+
| 1 | 0.07258075 | select foo_id, min(bar_id)-1 from mytable where foo_id = 1000 group by foo_id |
+----------+------------+-----------------------------------------------------------------------------------------------------------------+
I think something went wrong with MySQL's query planner. Is there any hint or something to force MySQL to use index for group by, when the number of foo_id
is only one? I tried analyze table mytable
but it doesn't help.
I know that a query select min(bar_id)-1 from mytable where foo_id = 1000
is fast when the number of foo_id
is only one, but it makes a branch to the code of my app, so I'd like to avoid this.
its not the answer. its only a sample from my comment
sample