MySQL排序后分頁(yè),因數(shù)據(jù)重復(fù)導(dǎo)致分頁(yè)數(shù)據(jù)紊亂的問題
背景
前不久在寫一個(gè)分頁(yè)接口的時(shí)候,在測(cè)試階段出現(xiàn)了排序結(jié)果紊亂且數(shù)據(jù)不正確的問題,那個(gè)接口是按照create_time進(jìn)行排序的,但是對(duì)應(yīng)的表中有很多相同create_time的數(shù)據(jù),最后發(fā)現(xiàn)是因?yàn)? order by 排序的時(shí)候,如果排序字段中有多行相同的列值,則排序結(jié)果是不確定的。
復(fù)現(xiàn)
創(chuàng)建一個(gè)簡(jiǎn)單表,并插入一些數(shù)據(jù)
- mysql> desc people;
- +-------------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+-------------+------+-----+---------+----------------+
- | id | bigint(20) | NO | PRI | NULL | auto_increment |
- | name | varchar(20) | NO | | NULL | |
- | create_time | bigint(20) | NO | | NULL | |
- +-------------+-------------+------+-----+---------+----------------+
- 3 行于數(shù)據(jù)集 (0.02 秒)
- mysql> select * from people;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 1 | 張三 | 1 |
- | 2 | 李四 | 2 |
- | 3 | 王五 | 3 |
- | 4 | 趙六 | 4 |
- | 5 | 孫七 | 2 |
- | 6 | 趙八 | 2 |
- | 7 | 吳九 | 2 |
- | 8 | 鄭十 | 2 |
- +----+--------+-------------+
- 8 行于數(shù)據(jù)集 (0.02 秒)
分頁(yè)的寫法
分頁(yè)一般有2個(gè)參數(shù):page:表示第幾頁(yè),從1開始,范圍[1,+∞)pageSize:每頁(yè)顯示多少條記錄,范圍[1,+∞)
limit分頁(yè)公式
(1)limit分頁(yè)公式:curPage是當(dāng)前第幾頁(yè);pageSize是一頁(yè)多少條記錄
limit (curPage-1)*pageSize,pageSize(2)用的地方:sql語(yǔ)句中
select 列 from 表名 limit(curPage-1)*pageSize,pageSize;
查詢復(fù)現(xiàn)
- mysql> select * from people order by create_time asc limit 0,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 1 | 張三 | 1 |
- | 2 | 李四 | 2 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.06 秒)
- mysql> select * from people order by create_time asc limit 2,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 8 | 鄭十 | 2 |
- | 6 | 趙八 | 2 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.09 秒)
- mysql> select * from people order by create_time asc limit 4,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 6 | 趙八 | 2 |
- | 7 | 吳九 | 2 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.04 秒)
- mysql> select * from people order by create_time asc limit 6,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 3 | 王五 | 3 |
- | 4 | 趙六 | 4 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.05 秒)
排序字段出現(xiàn)重復(fù)數(shù)據(jù),這時(shí)可以加入第二個(gè)排序字段,提高排序的唯一性,
- mysql> select * from people order by create_time asc,id asc limit 0,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 1 | 張三 | 1 |
- | 2 | 李四 | 2 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.05 秒)
- mysql> select * from people order by create_time asc,id asc limit 2,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 5 | 孫七 | 2 |
- | 6 | 趙八 | 2 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.10 秒)
- mysql> select * from people order by create_time asc,id asc limit 4,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 7 | 吳九 | 2 |
- | 8 | 鄭十 | 2 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.05 秒)
- mysql> select * from people order by create_time asc,id asc limit 6,2;
- +----+--------+-------------+
- | id | name | create_time |
- +----+--------+-------------+
- | 3 | 王五 | 3 |
- | 4 | 趙六 | 4 |
- +----+--------+-------------+
- 2 行于數(shù)據(jù)集 (0.03 秒)
我們可以觀察到第一次的查詢中,缺少了‘孫七’的數(shù)據(jù)行,當(dāng)我們加上了第二個(gè)排序字段時(shí)分頁(yè)數(shù)據(jù)變得正常了。
總結(jié)
MySQL 使用 limit 進(jìn)行分頁(yè)時(shí),可能會(huì)出現(xiàn)重復(fù)數(shù)據(jù),通過(guò)加入 order by 子句可以解決,但是需要注意的是,如果排序字段有相同值的情況下,由于排序字段數(shù)據(jù)重復(fù),可能會(huì)導(dǎo)致每次查詢排序后結(jié)果順序不同,分頁(yè)還是會(huì)出現(xiàn)重復(fù)數(shù)據(jù),這時(shí)可以加入第二個(gè)排序字段,提高排序的唯一性,最好保證排序的字段在表中的值是唯一的,這樣就可以少寫一個(gè)排序字段,增加查詢效率,因?yàn)? order by 后面有多個(gè)排序字段時(shí),無(wú)法用到索引。