Inconsistent costs for group by queries with and without index access
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
MariaDB |
Confirmed
|
Low
|
Timour Katchaounov |
Bug Description
The following example over the world database from the 5.3/mysql-
shows that a query that uses an index access to compute GROUP BY has the same cost
as the same query without index access (using temp table). At the same time the two
different query plans indeed show different number of Handler_read_* calls.
Test script:
create database world;
use world;
source include/
source include/world.inc
create index CityNmae on City(Name);
explain
SELECT name FROM City GROUP BY name HAVING Count(*) > 2;
flush status;
SELECT name FROM City GROUP BY name HAVING Count(*) > 2;
show status like 'last_query_cost';
show status like 'Handler_read%';
explain
SELECT name FROM City ignore index (cityname) GROUP BY name HAVING Count(*) > 2;
flush status;
SELECT name FROM City ignore index (cityname) GROUP BY name HAVING Count(*) > 2;
show status like 'last_query_cost';
show status like 'Handler_read%';
Changed in maria: | |
importance: | Medium → Low |
Changed in maria: | |
milestone: | 5.3 → 5.5 |
tags: | added: optimizer |
The output of the script above (edited for brevity):
MariaDB [world]> explain ------- -----+- ------+ ------- +------ ------- --+---- ------+ ------- --+---- --+---- --+---- ------- --+ ------- -----+- ------+ ------- +------ ------- --+---- ------+ ------- --+---- --+---- --+---- ------- --+ ------- -----+- ------+ ------- +------ ------- --+---- ------+ ------- --+---- --+---- --+---- ------- --+
-> SELECT name FROM City GROUP BY name HAVING Count(*) > 2;
+----+-
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-
| 1 | SIMPLE | City | index | NULL | CityName | 35 | NULL | 4080 | Using index |
+----+-
1 row in set (0.00 sec)
MariaDB [world]> flush status;
Query OK, 0 rows affected (0.00 sec)
MariaDB [world]> SELECT name FROM City GROUP BY name HAVING Count(*) > 2; ....... ....... ....... ..
+--------------+
| name |
+--------------+
| Cambridge |
.......
| Victoria |
+--------------+
13 rows in set (0.02 sec)
MariaDB [world]> show status like 'last_query_cost'; ------- ----+-- ------- ----+ ------- ----+-- ------- ----+ ------- ----+-- ------- ----+
+------
| Variable_name | Value |
+------
| Last_query_cost | 4944.815406 |
+------
1 row in set (0.01 sec)
MariaDB [world]> show status like 'Handler_read%'; ------- ------- ---+--- ----+ ------- ------- ---+--- ----+ read_rnd_ next | 0 | ------- ------- ---+--- ----+
+------
| Variable_name | Value |
+------
| Handler_read_first | 1 |
| Handler_read_key | 0 |
| Handler_read_next | 4080 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_
+------
6 rows in set (0.00 sec)
MariaDB [world]> ------- -----+- ------+ ------+ ------- ------- -+----- -+----- ----+-- ----+-- ----+-- ------- ------- ------- ------- ---+ ------- -----+- ------+ ------+ ------- ------- -+----- -+----- ----+-- ----+-- ----+-- ------- ------- ------- ------- ---+ ------- -----+- ------+ ------+ ------- ------- -+----- -+----- ----+-- ----+-- ----+-- ------- ------- ------- ------- ---+
MariaDB [world]> explain
-> SELECT name FROM City ignore index (cityname) GROUP BY name HAVING Count(*) > 2;
+----+-
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-
| 1 | SIMPLE | City | ALL | NULL | NULL | NULL | NULL | 4080 | Using temporary; Using filesort |
+----+-
1 row in set (0.00 sec)
MariaDB [world]> flush status;
Query OK, 0 rows affected (0.00 sec)
MariaDB [world]> SELECT name FROM City ignore index (cityname) GROUP BY name HAVING Count(*) > 2; ....... ....... ....
+--------------+
| name |
+--------------+
| Cambridge |
.......
| Victoria |
+--------------+
13 rows in set (0.02 sec)
MariaDB [world]> show status like 'last_query_cost'; ------- ----+-- ------- ----+ ------- ----+-- ------- ----+ ------- ----+-- ------- ----+
+------
| Variable_name | Value |
+------
| Last_query_cost | 4944.815406 |
+------
1 row in set (0.00 sec)
MariaDB [world]> show status like 'Handler_read%'; ------- ------- ---+--- ----+ ------- ------- ---+--- ----+
+------
| Variable_name | Value |
+------
| Handler_read_first | 0 |
| Handler_read_key | 4080 |
| Handler_read_next ...