对于tpch表中的orders表有如下查询:
select max(o_clerk),min(o_clerk) from (select o_clerk from orders order by o_clerk desc) a;
查看执行计划:
mysql> explain select max(o_clerk),min(o_clerk) from (select o_clerk from orders order by o_clerk desc) a;
+----------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+----------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------------+
| StreamAgg_8 | 1.00 | root | | funcs:max(tpch1.orders.o_clerk)->Column#10, funcs:min(tpch1.orders.o_clerk)->Column#11 |
| └─Sort_13 | 15000000.00 | root | | tpch1.orders.o_clerk:desc |
| └─TableReader_12 | 15000000.00 | root | | data:TableFullScan_11 |
| └─TableFullScan_11 | 15000000.00 | cop[tikv] | table:orders | keep order:false |
+----------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
可以看到还是做了Sort操作。
预期:优化器将语句进行改写为:select max(o_clerk),min(o_clerk) from orders;
预期执行计划为:
mysql> explain select max(o_clerk),min(o_clerk) from orders;
+----------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+----------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------------+
| StreamAgg_16 | 1.00 | root | | funcs:max(Column#14)->Column#10, funcs:min(Column#15)->Column#11 |
| └─TableReader_17 | 1.00 | root | | data:StreamAgg_8 |
| └─StreamAgg_8 | 1.00 | cop[tikv] | | funcs:max(tpch1.orders.o_clerk)->Column#14, funcs:min(tpch1.orders.o_clerk)->Column#15 |
| └─TableFullScan_15 | 15000000.00 | cop[tikv] | table:orders | keep order:false |
+----------------------------+-------------+-----------+---------------+----------------------------------------------------------------------------------------+
4 rows in set (0.01 sec)
该类问题是否可以进行优化增强?