您的位置:首页 > 数据库 > MySQL

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

2017-11-08 14:41 232 查看
一 介绍 
  本文源于生产过程中的案例,5.6版本的数据库使用limit和order by 一个非唯一字段时,结果集并不总是确定的.已经确定为bug,详见:MySQL
官方的bug 

二 分析 
环境准备 

CREATE TABLE `tb1` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `a` decimal(19,2) NOT NULL,
  `acid` bigint(20) NOT NULL,
  `prid` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_prid` (`prid`),
  KEY `idx_acid` (`acid`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

注意字段a 上面是没有索引的。
初始化数据

INSERT INTO `tb1` (`id`, `a`, `acid`, `prid`) 

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 排序

mysql> select * from tb1 order by a desc limit 4;

+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 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)

得到id 为10, 9, 14, 8 的结果集

mysql> select * from tb1 order by a desc limit 3;

+----+-------+------+------+
| id | a    | acid | prid |
+----+-------+------+------+
| 10 | 20.00 | 2    | 3    |
| 9  | 16.00 | 2    | 3    |
| 8  | 12.00 | 2    | 3    |
+----+-------+------+------+
3 rows in set (0.00 sec)

得到id 为10 9 8 的结果集

为a字段加上索引 

mysql> alter table tb1 add key ind_tb1a(a);

Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from tb1 order by a desc limit 3;
+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 10 | 20.00 | 2    | 3    |
| 9  | 16.00 | 2    | 3    |
| 8  | 12.00 | 2    | 3    |
+----+-------+------+------+
3 rows in set (0.00 sec)

得到id 为10 9 8 的结果集

mysql> select * from tb1 order by a desc limit 4;

+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 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)

得到id 为10, 9, 14, 8 的结果集

从上面的测试来看对于一个非唯一字段 无论是否含有索引,结果集都是不确定的。

三 解决方法 
1 业务属性确保 a 字段不能唯一,则需要针对排序结果再加上 一个唯一字段的排序 比如id 

mysql> select * from tb1 order by a desc ,id desc limit 4;

+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 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> select * from tb1 order by a desc ,id desc limit 3;

+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 10 | 20.00 | 2    | 3    |
| 9  | 16.00 | 2    | 3    |
| 14 | 12.00 | 2    | 4    |
+----+-------+------+------+
3 rows in set (0.00 sec)

使用order by id/unique_key 排序之后,前三个结果集是一致的10,9,14 。 结果集满足我们的需求。从而解决不确定性带来的问题。
2 是否可以去掉不必要的order by,这个是由业务逻辑决定的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息