关于mysql5.6 的排序问题.
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,mysql 5.6 的排序进行了优化. 同样的sql , 在5.5 跟5.6 上可能得到不同的结果:CREATE TABLE `TestCase2` (`id` bigint(20) NOT NULL
千家信息网最后更新 2025年11月07日关于mysql5.6 的排序问题.mysql 5.6 的排序进行了优化. 同样的sql , 在5.5 跟5.6 上可能得到不同的结果:
CREATE TABLE `TestCase2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`aValue` decimal(19,2) NOT NULL,
`accuracyClassType_id` bigint(20) NOT NULL,
`productType_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `FKF58064BD791396` (`productType_id`),
KEY `FKF58064BDED5076` (`accuracyClassType_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
INSERT INTO `TestCase2` (`id`, `aValue`, `accuracyClassType_id`, `productType_id`)
VALUES
(1,2.00,3,2),
(2,3.00,3,2),
(3,4.00,2,3),
(4,5.00,2,3),
(5,6.00,2,3),
(6,8.00,2,3),
(7,10.00,2,3),
(8,12.00,2,3),
(9,16.00,2,3),
(10,20.00,2,3),
(11,6.00,2,4),
(12,8.00,2,4),
(13,10.00,2,4),
(14,12.00,2,4),
(15,5.00,2,2),
(16,6.00,2,2);
select * from Testcase2 order by aValue desc;
# as you can see mysql has added a fallback sorting because aValue is not unique, which is ok
# the first four id's of the results are: 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 4;
# as expected the result id's (based on the order by) are : 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 3;
# which surprisingly results in the following id's based on the order by: 10, 9, 8 ???????
# expecting id's: 10, 9, 14 (see query with limit 4)[19 Mar 2014 13:34] Miguel SolorzanoThank you for the bug report.
mysql 5.1 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.1 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.5 跟 5.6 两个版本对排序的实现:
Mysql 5.5 willl
- allocate buffer space for 1000 rows
- scan the table, inserting each row into the buffer
- sort the buffer, this requires about 10.000 comparisons
- return the first 3 (or 4) rowsMysql
5.6 will
- allocate buffer space for 3 (or 4) rows
- scan the table, maintaining a collection of 3 (or 4) "winners" this requires about 2000 comparisons
- sort final set of "winners", this requires about 6 comparisons
- return the result
sql 标准里对于重复值的排序字段, 应该出哪行记录是没有定义的, 这就完全取决于代码的实现了.
我们的升级测试,可能会涉及到 5.5 与5.6 的结果对比, 这里可能会引起疑问.
这是正常的. 如果确实需要严格的排序,实现5.6 跟5.5 同样的显示结果.
需要修改sql 语句, 在排序子句中加上 一个唯一字段.
参考:
https://bugs.mysql.com/bug.php?spm=5176.100239.blogcont27649.4.tlI76p&id=72076
http://didrikdidrik.blogspot.co.uk/
CREATE TABLE `TestCase2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`aValue` decimal(19,2) NOT NULL,
`accuracyClassType_id` bigint(20) NOT NULL,
`productType_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `FKF58064BD791396` (`productType_id`),
KEY `FKF58064BDED5076` (`accuracyClassType_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
INSERT INTO `TestCase2` (`id`, `aValue`, `accuracyClassType_id`, `productType_id`)
VALUES
(1,2.00,3,2),
(2,3.00,3,2),
(3,4.00,2,3),
(4,5.00,2,3),
(5,6.00,2,3),
(6,8.00,2,3),
(7,10.00,2,3),
(8,12.00,2,3),
(9,16.00,2,3),
(10,20.00,2,3),
(11,6.00,2,4),
(12,8.00,2,4),
(13,10.00,2,4),
(14,12.00,2,4),
(15,5.00,2,2),
(16,6.00,2,2);
select * from Testcase2 order by aValue desc;
# as you can see mysql has added a fallback sorting because aValue is not unique, which is ok
# the first four id's of the results are: 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 4;
# as expected the result id's (based on the order by) are : 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 3;
# which surprisingly results in the following id's based on the order by: 10, 9, 8 ???????
# expecting id's: 10, 9, 14 (see query with limit 4)[19 Mar 2014 13:34] Miguel SolorzanoThank you for the bug report.
mysql 5.1 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.1 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.5 跟 5.6 两个版本对排序的实现:
Mysql 5.5 willl
- allocate buffer space for 1000 rows
- scan the table, inserting each row into the buffer
- sort the buffer, this requires about 10.000 comparisons
- return the first 3 (or 4) rowsMysql
5.6 will
- allocate buffer space for 3 (or 4) rows
- scan the table, maintaining a collection of 3 (or 4) "winners" this requires about 2000 comparisons
- sort final set of "winners", this requires about 6 comparisons
- return the result
sql 标准里对于重复值的排序字段, 应该出哪行记录是没有定义的, 这就完全取决于代码的实现了.
我们的升级测试,可能会涉及到 5.5 与5.6 的结果对比, 这里可能会引起疑问.
这是正常的. 如果确实需要严格的排序,实现5.6 跟5.5 同样的显示结果.
需要修改sql 语句, 在排序子句中加上 一个唯一字段.
参考:
https://bugs.mysql.com/bug.php?spm=5176.100239.blogcont27649.4.tlI76p&id=72076
http://didrikdidrik.blogspot.co.uk/
排序
结果
字段
不同
两个
代码
取决于
子句
标准
版本
疑问
语句
这是
中加
升级
参考
测试
问题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
asp页面连接数据库失败
mac 下数据库建模工具
哪里的软件开发好
怎么往数据库表中加入数据
简述数据库镜像技术及其用途
中介有维护网络安全责任吗
5e对战平台进不去对局服务器
网络安全对国家安全的关系
数据库id自增长删除后
csgo社区服务器皮肤体验指令
关于网络安全的故事
苏州有哪些打车软件开发
洛阳软件开发公司合作
求生之路重启服务器指令
浙江加工刀片服务器销售
防汛网络安全预案
托管服务器
企业无线网络安全威胁
吱信(上海)网络技术有限公司给我转账
上哪租服务器
国外服务器供应商
广西移动城管软件开发专业制作
房地一体数据库汇总
rh2288v3服务器升级
网络安全知识竞赛的证书
京东2005年用的服务器
如何加强无线网络安全
相同服务器机器压力大一倍
我的世界服务器存档转单机存档
问卷调查如何录数据库