MySQL order by 排序結果不正確

haoge0205發表於2020-04-01

MySQL order by 排序結果不正確

新建一張測試表:

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結果集有比較高的嚴格要求 ,請記得利用唯一鍵排序。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-2683988/,如需轉載,請註明出處,否則將追究法律責任。

相關文章