MySQL order by 排序結果不正確
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORDER對查詢結果進行排序排序
- MySQL bit型別增加索引後查詢結果不正確案例淺析MySql型別索引
- MYSQL order by排序與索引關係總結MySql排序索引
- MySQL Order BY 排序過程MySql排序
- MySQL distinct 和 order by 排序混淆的替代方案MySql排序
- MySQL按指定順序排序(order by field的使用)MySql排序
- MYSQL order by排序導致效率低小優化MySql排序優化
- order by改分組排序排序
- MySQL:關於排序order by limit值不穩定的說明(1)MySql排序MIT
- MySQL 之 ORDER BY FIELDMySql
- mysql order by 優化MySql優化
- mysql怎麼設定密碼都不正確的一個解決方法MySql密碼
- keycloak~時間不正確的問題
- Jni CallVoidMethod回撥傳值不正確
- MySql ORDER BY索引是否失效MySql索引
- MySQL, Incorrect usage of UNION and ORDER BYMySql
- Elasticsearch聚合學習之四:結果排序Elasticsearch排序
- 瀏覽器證書機構不正確瀏覽器
- 數字型別的不正確轉換漏洞型別
- 相對論為什麼不正確呢?
- Mysql系列第八講 詳解排序和分頁(order by & limit)及存在的坑MySql排序MIT
- 單表掃描,MySQL索引選擇不正確 並 詳細解析OPTIMIZER_TRACE格式MySql索引
- MySQL 5.7:desc order by反向定位MySql
- MySQL——優化ORDER BY語句MySql優化
- mysql group by 取想要的結果MySql
- thinkphp where in order 按照順序in的迴圈排序PHP排序
- 快排實現仿order by多欄位排序排序
- MySQL查詢結果匯出方式總結MySql
- Laravel 根據 relation sum 結果排序的小技巧Laravel排序
- MySQL LIMIT 和 ORDER BY 最佳化MySqlMIT
- mysql order by是怎麼工作的?MySql
- easycode生成程式碼報錯:配置資訊不正確
- win10 office元件安裝不正確怎麼辦_win10電腦中office元件安裝不正確的解決教程Win10元件
- MySQL:MySQL客戶端快取結果導致OOMMySql客戶端快取OOM
- mysql order by 和 group by 順序問題MySql
- mysql ,tidb sysbench 測試結果記錄MySqlTiDB
- Mysql按指定 ID 順序返回結果MySql
- MySQL 按指定 ID 順序返回結果MySql