Open Alkaagr81 opened 3 years ago
it can be optimized by logic plan, when sum
encounter order by
, we can prune the order by
, make it a simple query.
any suggestions or duplicated task? @winoros @XuHuaiyu
mysql > explain SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
Mon Nov 29 20:20:01 2021
mysql > show warnings;
+-------+------+-------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------+
| Note | 1003 | /* select#1 */ select sum(`test`.`t1`.`a`) AS `SUM(a)` from `test`.`t1` |
+-------+------+-------------------------------------------------------------------------+
1 row in set (0.00 sec)
after order by FD
mysql> SELECT a FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 3029 (HY000): Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t2.a) FROM t1 AS t2);
+--------+
| SUM(a) |
+--------+
| 3 |
+--------+
1 row in set (0.00 sec)
Bug Report
Please answer these questions before submitting your issue. Thanks!
1. Minimal reproduce step (Required)
2. What did you expect to see? (Required)
3. What did you see instead (Required)
4. What is your TiDB version? (Required)