mysql 5.7.15 union order by 子查询排序不生效
解决办法:添加limit,解析器不会优化order by字段
1 | (SELECT t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 6 ORDER BY vc_operdate DESC LIMIT 999999 ) |
详细介绍:
理论上的查询语句正确写法1
2
3
4
5(SELECT t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 6 ORDER BY vc_operdate DESC )
UNION
(select t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 1 ORDER BY vc_operdate DESC )
UNION
(SELECT t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 2 ORDER BY vc_operdate DESC )
主要完成的是TBatch表中按照c_state 显示6然后1最后2的先后分贝按照操作时间排序,返回分页结果集。
查询结果按照c_state 先后union在了一起,但是每个状态中并没有按照时间排序。排序失效
查看执行过程,SQL被解释器优化后的查询语句
查看执行计划:EXPLAIN …1
2
3
4
5EXPLAIN (SELECT t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 6 ORDER BY vc_operdate DESC )
UNION
(SELECT t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 1 ORDER BY vc_operdate DESC )
UNION
(SELECT t.*,t.vc_userid AS operateid FROM TBatch t WHERE c_state = 2 ORDER BY vc_operdate DESC )
查看解释器优化后的SQL:SHOW WARNINGS
1 | SHOW WARNINGS |
复制解释器中message字段就是实际执行的SQL
1 | ( |
通过查看sql的执行计划和解析器优化后的执行SQL发现,子查询中根本不带 order by 查询,很苦恼。
经过查询解决办法,发现 如果order by 不带limit,会被优化器干掉order by 字段。
解决方案:
1 | EXPLAIN |
1 | SHOW WARNINGS |
1 | ( |
参考资料:https://blog.csdn.net/xihuanqiqi/article/details/73550783