MySQL 分页数据错乱重复
select xx from table_name wheere xxx order by 字段A limit offset;
,
表数据总共 48 条,分页数量正常,但出现了结果混杂的情况,第一页的数据出现在了第二页上;如果 order by 字段B
就不会出现这个现象,怎么会这样呢!
If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.
One factor that affects the execution plan is LIMIT, so an ORDER BY query with and without LIMIT may return rows in different orders.
问题重现
本次实验使用社区版 MySQL 5.6.26(因为小明出现问题的环境就是这个版本O(∩_∩)O~),下面先创建实验环境和初始化测试数据:
root@localhost [(none)]>select @@version;
+------------+
| @@version |
+------------+
| 5.6.26-log |
+------------+
1 row in set (0.00 sec)
root@localhost [(none)]>show variables like "sql_mode";
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| sql_mode | NO_ENGINE_SUBSTITUTION |
+---------------+------------------------+
1 row in set (0.00 sec)
root@localhost [(none)]>create database glon_ho;
Query OK, 1 row affected (0.04 sec)
root@localhost [(none)]>use glon_ho
Database changed
root@localhost [glon_ho]>create table glon(
-> id int not null auto_increment primary key,
-> name varchar(20) not null,
-> create_time datetime not null,
-> age tinyint unsigned default 18
-> );
Query OK, 0 rows affected (0.01 sec)
root@localhost [glon_ho]>INSERT INTO `glon` VALUES (1, 'Eason Chan', '2017-05-02 08:10:10', 19),(2, 'Glon Ho', '2017-05-03 12:10:10', 18),(3, '赵敏', '2017-05-03 14:10:10', 17),(4, 'Jacky Cheung', '2017-05-02 14:00:00', 22),(5, '周芷若', '2017-05-02 14:00:00', 16),(6, 'Andy Lau', '2017-05-02 14:00:00', 50),(7, '至尊宝', '2017-05-02 14:00:00', 20),(8, '刘三姐', '2017-05-02 14:00:00', 19);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
root@localhost [glon_ho]>select * from glon;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
+----+--------------+---------------------+------+
8 rows in set (0.00 sec)
这里创建了一个 glon 表,字段有自增 id, 姓名 name, 年龄 age, 及用户注册时间 create_time。
接着来复现问题:
- 根据用户注册时间 create_time 来排序:
root@localhost [glon_ho]>select * from glon ORDER BY create_time limit 0, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
可以看到两次查询结果中都出现了 id 为 8 的刘三姐,从上面初始化数据来看,总共有 8 条数据,现在不但分页出现重复数据,还丢了一条!
问题确实重现了,不过先不急,我们再来试多几组其他的排序方式。
- create_time 和 age 组合排序
root@localhost [glon_ho]>select * from glon ORDER BY create_time,age limit 0, 4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,age limit 4, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
- create_time 和 id 组合排序
root@localhost [glon_ho]>select * from glon ORDER BY create_time,id limit 0, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,id limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
- 主键 id 排序
root@localhost [glon_ho]>select * from glon ORDER BY id limit 0, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY id limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
看到,后面的几组排序方式都没有再出现问题了,结合官档,我们知道 order by 排序的时候,如果排序字段中有多行相同的列值,则排序结果是不确定的。所以后面的几组组合形式的排序或者是主键 id 的排序,因为唯一性高,所以排序是确定的,不会出现结果混乱的问题。
那是不是可以就此结束了呢,no way, 我们再来看下面的实验,继续巩固一下:
- 根据年龄 age 来排序:
root@localhost [glon_ho]>select * from glon ORDER BY age limit 0, 4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY age limit 4, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
咦,这个排序也只是根据一个字段 age 来排序,怎么就没有出问题呢?不急,还有招:
root@localhost [glon_ho]>insert into glon values (9,'乔峰','2017-05-03 13:10:10',22),(10,'段誉','2017-05-03 15:10:10',19),(11,'郭靖','2017-05-03 17:10:10',20),(12,'黄蓉','2017-05-03 08:10:10',19);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
root@localhost [glon_ho]>select * from glon;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 11 | 郭靖 | 2017-05-03 17:10:10 | 20 |
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
+----+--------------+---------------------+------+
12 rows in set (0.00 sec)
我又给 glon 表新增了几条数据,然后再来看看:
root@localhost [glon_ho]>select * from glon ORDER BY create_time limit 0, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time limit 8, 4;
+----+--------+---------------------+------+
| id | name | create_time | age |
+----+--------+---------------------+------+
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 11 | 郭靖 | 2017-05-03 17:10:10 | 20 |
+----+--------+---------------------+------+
4 rows in set (0.00 sec)
根据 create_time 排序,没有问题了,再来:
root@localhost [glon_ho]>select * from glon ORDER BY age limit 0, 4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY age limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY age limit 8, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
可以看到根据年龄 age 排序,问题出现了。
然后在看看组合的排序:
root@localhost [glon_ho]>select * from glon ORDER BY create_time,id limit 0, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,id limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,id limit 8, 4;
+----+--------+---------------------+------+
| id | name | create_time | age |
+----+--------+---------------------+------+
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 11 | 郭靖 | 2017-05-03 17:10:10 | 20 |
+----+--------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,age limit 0, 4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,age limit 4, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY create_time,age limit 8, 4;
+----+--------+---------------------+------+
| id | name | create_time | age |
+----+--------+---------------------+------+
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 11 | 郭靖 | 2017-05-03 17:10:10 | 20 |
+----+--------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY age,id limit 0, 4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY age,id limit 4, 4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon ORDER BY age,id limit 8, 4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 11 | 郭靖 | 2017-05-03 17:10:10 | 20 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
思考
既然排序不定,那么给排序字段加上索引会不会有用呢?
root@localhost [glon_ho]>alter table glon add index ix_age(age);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@localhost [glon_ho]>show create table glon\G
*************************** 1. row ***************************
Table: glon
Create Table: CREATE TABLE `glon` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`create_time` datetime NOT NULL,
`age` tinyint(3) unsigned DEFAULT '18',
PRIMARY KEY (`id`),
KEY `ix_age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
root@localhost [glon_ho]>select * from glon order by age limit 0,4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon order by age limit 4,4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from glon order by age limit 8,4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
也可以不在 order by 后面加多一列增加唯一性,可以改写成下面的形式:
root@localhost [glon_ho]>select * from (select distinct g.* from glon g order by age) t limit 0,4;
+----+------------+---------------------+------+
| id | name | create_time | age |
+----+------------+---------------------+------+
| 5 | 周芷若 | 2017-05-02 14:00:00 | 16 |
| 3 | 赵敏 | 2017-05-03 14:10:10 | 17 |
| 2 | Glon Ho | 2017-05-03 12:10:10 | 18 |
| 1 | Eason Chan | 2017-05-02 08:10:10 | 19 |
+----+------------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from (select distinct g.* from glon g order by age) t limit 4,4;
+----+-----------+---------------------+------+
| id | name | create_time | age |
+----+-----------+---------------------+------+
| 10 | 段誉 | 2017-05-03 15:10:10 | 19 |
| 8 | 刘三姐 | 2017-05-02 14:00:00 | 19 |
| 12 | 黄蓉 | 2017-05-03 08:10:10 | 19 |
| 11 | 郭靖 | 2017-05-03 17:10:10 | 20 |
+----+-----------+---------------------+------+
4 rows in set (0.00 sec)
root@localhost [glon_ho]>select * from (select distinct g.* from glon g order by age) t limit 8,4;
+----+--------------+---------------------+------+
| id | name | create_time | age |
+----+--------------+---------------------+------+
| 7 | 至尊宝 | 2017-05-02 14:00:00 | 20 |
| 9 | 乔峰 | 2017-05-03 13:10:10 | 22 |
| 4 | Jacky Cheung | 2017-05-02 14:00:00 | 22 |
| 6 | Andy Lau | 2017-05-02 14:00:00 | 50 |
+----+--------------+---------------------+------+
4 rows in set (0.00 sec)
总之,如果发生了,最简单的方法就是在排序列(如 create time)上加索引,然后在 order by 上明示 primary key,这个问题就非常圆满的解决了。
总结
MySQL 使用 limit 进行分页时,可能会出现重复数据,通过加入 order by 子句可以解决,但是需要注意的是,如果排序字段有相同值的情况下,由于排序字段数据重复,可能会导致每次查询排序后结果顺序不同,分页还是会出现重复数据,这时可以加入第二个排序字段,提高排序的唯一性,最好保证排序的字段在表中的值是唯一的,这样就可以少写一个排序字段,增加查询效率,因为 order by 后面有多个排序字段时,无法用到索引。
推荐 2015年 06 月《数据库内核月报》的一篇旧文:MySQL · 答疑解惑 · MySQL Sort 分页
MySQL 分页数据错乱重复的更多相关文章
- 解决 MySQL 分页数据错乱重复
前言 一天,小明兴匆匆的在通讯工具上说:这边线上出现了个奇怪的问题,麻烦 DBA 大大鉴定下,执行语句 select xx from table_name wheere xxx order by 字段 ...
- mysql 分页数据错乱
最近在使用mysql 分页查询数据的时候发现返回的数据与预期的不一样,显示数据重复错乱. 在官方文档 有这样一句话 If multiple rows have identical values in ...
- EF获取多个数据集以及MySQL分页数据查询优化
背景:MySQL分页查询语句为 ,10; 一般页面还会获取总条数,这时候还需要一条查询总条数语句 , 这样数据库需要执行两次查询操作.MySQL提供了SQL_CALC_FOUND_ROWS追踪总条数的 ...
- Mysql 删除数据表重复行
准备示例数据 以下sql创建表,并将示例数据插入到用于演示的contacts表中. CREATE TABLE contacts ( id INT PRIMARY KEY AUTO_INCREMENT, ...
- SpringMVC+Mybatis实现的Mysql分页数据查询
周末这天手痒,正好没事干,想着写一个分页的例子出来给大家分享一下. 这个案例分前端和后台两部分,前端使用面向对象的方式写的,里面用到了一些回调函数和事件代理,有兴趣的朋友可以研究一下.后台的实现技术是 ...
- github PageHelper使用PageInfo数据错乱问题
如果出现了数据错乱,检查一下代码是否正确 PageHelper.start(1,10); List<MyEntity> lists = mapper.query(); PageInfo&l ...
- MySQL大数据分页的优化思路和索引延迟关联
之前上次在部门的分享会上,听了关于MySQL大数据的分页,即怎样使用limit offset,N来进行大数据的分页,现在做一个记录: 首先我们知道,limit offset,N的时候,MySQL的查询 ...
- mysql插入数据与删除重复记录的几个例子(收藏)
mysql插入数据与删除重复记录的几个例子 12-26shell脚本实现mysql数据的批量插入 12-26mysql循环语句插入数据的例子 12-26mysql批量插入数据(insert into ...
- iOS不得姐项目--推荐关注模块(一个控制器控制两个tableView),数据重复请求的问题,分页数据的加载,上拉下拉刷新(MJRefresh)
一.推荐关注模块(一个控制器控制两个tableView) -- 数据的显示 刚开始加载数据值得注意的有以下几点 导航控制器会自动调整scrollView的contentInset,最好是取消系统的设置 ...
随机推荐
- pygame-KidsCanCode系列jumpy-part14-背景音乐及音效
没有音乐和音效的游戏是没有灵魂的,这回讲解如何处理背景音乐及跳跃音效.加载music及sound的方法,之前已经写过,见:pygame 笔记-8 背景音乐&子弹音效 . 先介绍一个很棒的生成各 ...
- tensorflow由于未初始化变量所导致的错误
版权声明:本文为博主原创文章,如需转载请注明出处,谢谢. https://blog.csdn.net/qq_38542085/article/details/78742295 初始代码 import ...
- 搜索历史命令 Ctrl + R ( ctrl + r to search the history command )
Linux下的神器 ctrl + r (reverse-i-search ) 的使用方法: (reverse-i-search usage: ) (press ctl + r ) 输入任意字符,例 ...
- Coursera机器学习+deeplearning.ai+斯坦福CS231n
日志 20170410 Coursera机器学习 2017.11.28 update deeplearning 台大的机器学习课程:台湾大学林轩田和李宏毅机器学习课程 Coursera机器学习 Wee ...
- 使用wrk进行压力测试
最近需要对新的服务进行压力测试.比较了ab和jemeter以及wrk.最终选择wrk来作为压力测试工具,可以把cpu压到100%. 官方源码: https://github.com/wg/wrk 安装 ...
- MyBatis(五):mybatis关联映射
Mybatis中表与表之间的关系分为一下4类: 1)一对一 2)一对多 3)多对一 4)多对多 创建数据Demo表 数据库表: 用户表user:记录了购买商品的用户信息. 订单表orders:记录了用 ...
- 终止java线程的2种方法
1.使用一个volatile的共享变量 2.使用interrupt方法 import java.util.concurrent.TimeUnit; /** * ThreadTest */ public ...
- ShrePoint 迁移域控
背景: 客户的AD服务器换掉,重新安装AD,SharePoint服务器重新加入域后,将所有服务账号换成新域的账号. 虽然SP的应用程序采用的是表单认证,但是,用户(包括管理员)访问站点,添加数据的时候 ...
- Docker Mongo数据库开启用户认证
一.启动mongo容器的几种方式 #简化版 docker run --name mongo1 -p 21117:27017 -d mongo --noprealloc --smallfiles #自定 ...
- 面试汇总——说一下CSS盒模型
本文是面试汇总分支——说一下CSS盒模型. 基本概念:W3C标准盒模型和IE盒模型 CSS如何设置这两种模型 JS如何获取盒模型对应的宽和高 根据盒模型解释边距重叠 BFC(边距重叠解决方案) 一. ...