Bug #4402 Equivalence is not considered in group by and order by
Submitted: 4 Jul 2004 8:21 Modified: 3 Oct 2008 8:36
Reporter: Peter Zaitsev (Basic Quality Contributor) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S4 (Feature request)
Version:4.1, 5.1.28, 6.0.6 OS:Any (all)
Assigned to: CPU Architecture:Any
Tags: qc

[4 Jul 2004 8:21] Peter Zaitsev
Description:
Hi,

This issue mostly gives problems in joins where if optimizer selects table order with 
group by table being second  temporary/filesort is used without need.

mysql> explain select count(*) from a where  i=j group by j order by j desc limit 1;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                        |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
|  1 | SIMPLE      | a     | ALL  | NULL          | NULL |    NULL | NULL |    7 | Using where; Using temporary; Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
1 row in set (0.00 sec)

mysql> explain select count(*) from a where  i=j group by i order by i desc limit 1;
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | a     | index | NULL          | i    |       5 | NULL |    7 | Using where |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

These queries are equivalent but yet use different execution plans.    For  reference however
we detect i=j  clause:

mysql> explain select * from a where j=5 and i=j;
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | a     | ref  | i             | i    |       5 | const |    1 | Using where |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from a where i=5 and i=j;
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | a     | ref  | i             | i    |       5 | const |    1 | Using where |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
1 row in set (0.00 sec)

How to repeat:
CREATE TABLE a (
  i int(11) default NULL,
  j int(11) default NULL,
  KEY i (i)
) ;

INSERT INTO a VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6);

explain select count(*) from a where  i=j group by j order by j desc limit 1;
[4 Jul 2004 10:46] Sergei Golubchik
it's definitely a feature request, and not a small one.
[4 Jul 2004 18:26] Peter Zaitsev
I would expect you to move it to Feature Requests,
however I think in this case docs should be updated that  we have transformation to handle
equivalent columns  in where clause only.
[3 Oct 2008 8:36] Valeriy Kravchuk
Thank you for a reasonable feature request.