MySQL order by 排序結果不正確

2022-05-07 03:09:08 字數 3703 閱讀 4796

新建一張測試表:

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);

檢視表資料:

([yoon]> select * from

tb1;

+----+-------+------+------+

| id | a | acid | prid |

+----+-------+------+------+

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

+----+-------+------+------+

根據非索引欄位且有重複資料的字段 a 進行 order by 排序:

([yoon]> select * from

tb1 order by a desc ;

+----+-------+------+------+

| id | a | acid | prid |

+----+-------+------+------+

| 10 | 20.00 | 2 | 3 |

| 9 | 16.00 | 2 | 3 |

| 14 | 12.00 | 2 | 4 |

| 8 | 12.00 | 2 | 3 |

| 13 | 10.00 | 2 | 4 |

| 7 | 10.00 | 2 | 3 |

| 12 | 8.00 | 2 | 4 |

| 6 | 8.00 | 2 | 3 |

| 11 | 6.00 | 2 | 4 |

| 16 | 6.00 | 2 | 2 |

| 5 | 6.00 | 2 | 3 |

| 4 | 5.00 | 2 | 3 |

| 15 | 5.00 | 2 | 2 |

| 3 | 4.00 | 2 | 3 |

| 2 | 3.00 | 3 | 2 |

| 1 | 2.00 | 3 | 2 |

+----+-------+------+------+

order by 和 limit 一起使用:

([yoon]> 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 |

+----+-------+------+------+

為 a 字段新增索引:

([yoon]>alter table tb1 add index idx_a(a);

query ok,

0 rows affected (0.05 sec)

([yoon]> 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 |

+----+-------+------+------+

排序的時候再新增乙個字段 id :

([yoon]> 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 |

+----+-------+------+------+

對於乙個非唯一字段,無論是否含有索引,結果集都是不確定的。如果業務邏輯對分頁或者order by結果集有比較高的嚴格要求 ,請記得利用唯一鍵排序。

mysql order by 排序技巧

首先我們新建表test,如下 create table test id int 11 not null auto increment,name varchar 255 default null,primary key id engine innodb auto increment 7 default...

mysql order by 排序索引

接手別人的 遇到乙個客戶需求,說介面查詢較慢,需要進行優化。後面通過列印執行時間定位到是某一句sql執行較慢。sql如下 select from t base alarm tba where1 1and tba.rule type 1order by alarm status asc end ts ...

Mysql order by 多欄位排序

降序desc 由大到小 公升序asc 由小到大 mysql單個字段降序排序 select from table order by id desc mysql單個字段公升序排序 select from table order by id asc mysql多個字段排序 select from tabl...