【MySQL】order by 结果不准确的问题及解决

一 介绍 
  相信作为DBA 在和开发打交道的过程中,经常会遇到分页查询 order by 排序这样的需求。本文源于生产过程中的案例,5.6版本的数据库使用limit和order by 一个非唯一字段时,结果集并不总是确定的.已经确定为bug,详见:MySQL 官方的bug 
提醒读者朋友注意。
二 分析 
环境准备 
  1. CREATE TABLE `tb1` (
  2.   `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3.   `a` decimal(19,2) NOT NULL,
  4.   `acid` bigint(20) NOT NULL,
  5.   `prid` bigint(20) NOT NULL,
  6.   PRIMARY KEY (`id`),
  7.   KEY `idx_prid` (`prid`),
  8.   KEY `idx_acid` (`acid`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
注意字段a 上面是没有索引的。
初始化数据
  1. INSERT INTO `tb1` (`id`, `a`, `acid`, `prid`)
  2. 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);
执行两个 根据非索引字段且有重复值的 order by 排序
  1. mysql> select * from tb1 order by a desc limit 4;
  2. +----+-------+------+------+
  3. | id | a     | acid | prid |
  4. +----+-------+------+------+
  5. | 10 | 20.00 | 2    | 3    |
  6. | 9  | 16.00 | 2    | 3    |
  7. | 14 | 12.00 | 2    | 4    |
  8. | 8  | 12.00 | 2    | 3    |
  9. +----+-------+------+------+
  10. 4 rows in set (0.00 sec)
得到id 为10, 9, 14, 8 的结果集
  1. mysql> select * from tb1 order by a desc limit 3;
  2. +----+-------+------+------+
  3. | id | a     | acid | prid |
  4. +----+-------+------+------+
  5. | 10 | 20.00 | 2    | 3    |
  6. | 9  | 16.00 | 2    | 3    |
  7. | 8  | 12.00 | 2    | 3    |
  8. +----+-------+------+------+
  9. 3 rows in set (0.00 sec)
得到id 为10 9 8 的结果集
为a字段加上索引 
  1. mysql> alter table tb1 add key ind_tb1a(a);
  2. Query OK, 0 rows affected (0.00 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> select * from tb1 order by a desc limit 3;
  5. +----+-------+------+------+
  6. | id | a     | acid | prid |
  7. +----+-------+------+------+
  8. | 10 | 20.00 | 2    | 3    |
  9. | 9  | 16.00 | 2    | 3    |
  10. | 8  | 12.00 | 2    | 3    |
  11. +----+-------+------+------+
  12. 3 rows in set (0.00 sec)
得到id 为10 9 8 的结果集
  1. mysql> select * from tb1 order by a desc limit 4;
  2. +----+-------+------+------+
  3. | id | a     | acid | prid |
  4. +----+-------+------+------+
  5. | 10 | 20.00 | 2    | 3    |
  6. | 9  | 16.00 | 2    | 3    |
  7. | 14 | 12.00 | 2    | 4    |
  8. | 8  | 12.00 | 2    | 3    |
  9. +----+-------+------+------+
  10. 4 rows in set (0.00 sec)
得到id 为10, 9, 14, 8 的结果集
从上面的测试来看对于一个非唯一字段 无论是否含有索引,结果集都是不确定的。

三 解决方法 
1 业务属性确保 a 字段不能唯一,则需要针对排序结果再加上 一个唯一字段的排序 比如id 
  1. mysql> select * from tb1 order by a desc ,id desc limit 4;
  2. +----+-------+------+------+
  3. | id | a     | acid | prid |
  4. +----+-------+------+------+
  5. | 10 | 20.00 | 2    | 3    |
  6. | 9  | 16.00 | 2    | 3    |
  7. | 14 | 12.00 | 2    | 4    |
  8. | 8  | 12.00 | 2    | 3    |
  9. +----+-------+------+------+
  10. 4 rows in set (0.00 sec)
  1. mysql> select * from tb1 order by a desc ,id desc limit 3;
  2. +----+-------+------+------+
  3. | id | a     | acid | prid |
  4. +----+-------+------+------+
  5. | 10 | 20.00 | 2    | 3    |
  6. | 9  | 16.00 | 2    | 3    |
  7. | 14 | 12.00 | 2    | 4    |
  8. +----+-------+------+------+
  9. 3 rows in set (0.00 sec)
使用order by id/unique_key 排序之后,前三个结果集是一致的10,9,14 。  结果集满足我们的需求。从而解决不确定性带来的问题。
2 是否可以去掉不必要的order by,这个是由业务逻辑决定的。

四 总结
  DBA在和开发沟通/培训过程中要注意这一点 基于非唯一字段的排序 结果集是不确定的。如果业务逻辑对分页或者order by结果集有比较高的严格要求 ,请记得利用唯一键排序。 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-1825723/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22664653/viewspace-1825723/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:根据引用内容中的描述,MySQL的子查询中加入order by语句可能会导致索引失效。在MySQL 5.7之后,为了解决这个问题,需要在子查询中加入limit限制。然而,即使加入了limit,仍然可能会使用filesort,导致子查询无效。因此,使用子查询进行order by操作可能会遇到问题。\[1\] 引用\[2\]:在MySQL中,建立索引是非常重要的。常见的问题之一是询问什么情况下索引会失效。一般来说,索引失效可能是因为没有遵守B+树的最左原则。此外,即使按照最左原则建立了索引,有时也会遇到使用order by时走索引和不走索引的情况。\[2\] 引用\[3\]:根据引用内容中的描述,当查询字段为'*'时,MySQL可能会选择全表扫描而不走索引。这是因为当查询字段为'*'时,MySQL需要进行回表查询,即在返回之前还需要进行多次回表操作。然而,如果查询字段为对应的索引字段,MySQL可以直接通过索引拿到对应的返回字段,不需要进行回表操作,从而提高查询速度。此外,B+树的叶子节点已经排好序,也不需要进行排序操作,进一步提高了查询效率。因此,当查询字段为对应索引字段时,MySQL会选择走索引。\[3\] 综上所述,当使用order by时,MySQL的索引可能会失效。这可能是因为子查询中加入order by导致索引失效,或者查询字段为'*'时MySQL选择全表扫描而不走索引。在实际使用中,需要注意这些情况,合理设计索引和查询语句,以提高查询效率。 #### 引用[.reference_title] - *1* [MySQLorder by不使用索引的解决办法](https://blog.csdn.net/weixin_38238552/article/details/102530479)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [什么情况下mysql使用order by不会走索引?](https://blog.csdn.net/qq_38258642/article/details/129035223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值