Details
Description
The following example over the world database from the 5.3/mysql-test/include directory
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/world_schema.inc 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%';
Gliffy Diagrams
Attachments
Activity
- All
- Comments
- Work Log
- History
- Activity
- Transitions
Re: Inconsistent costs for group by queries with and without index access
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]> 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)