關於mysql5.6 的排序問題.
mysql 5.6 的排序進行了最佳化. 同樣的sql , 在5.5 跟5.6 上可能得到不同的結果:
CREATE TABLE `TestCase2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`aValue` decimal(19,2) NOT NULL,
`accuracyClassType_id` bigint(20) NOT NULL,
`productType_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `FKF58064BD791396` (`productType_id`),
KEY `FKF58064BDED5076` (`accuracyClassType_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
INSERT INTO `TestCase2` (`id`, `aValue`, `accuracyClassType_id`, `productType_id`)
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);
select * from Testcase2 order by aValue desc;
# as you can see mysql has added a fallback sorting because aValue is not unique, which is ok
# the first four id's of the results are: 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 4;
# as expected the result id's (based on the order by) are : 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 3;
# which surprisingly results in the following id's based on the order by: 10, 9, 8 ???????
# expecting id's: 10, 9, 14 (see query with limit 4)[19 Mar 2014 13:34] Miguel SolorzanoThank you for the bug report.
mysql 5.1 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.1 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.5 跟 5.6 兩個版本對排序的實現:
Mysql 5.5 willl
- allocate buffer space for 1000 rows
- scan the table, inserting each row into the buffer
- sort the buffer, this requires about 10.000 comparisons
- return the first 3 (or 4) rowsMysql
5.6 will
- allocate buffer space for 3 (or 4) rows
- scan the table, maintaining a collection of 3 (or 4) "winners" this requires about 2000 comparisons
- sort final set of "winners", this requires about 6 comparisons
- return the result
sql 標準裡對於重複值的排序欄位, 應該出哪行記錄是沒有定義的, 這就完全取決於程式碼的實現了.
我們的升級測試,可能會涉及到 5.5 與5.6 的結果對比, 這裡可能會引起疑問.
這是正常的. 如果確實需要嚴格的排序,實現5.6 跟5.5 同樣的顯示結果.
需要修改sql 語句, 在排序子句中加上 一個唯一欄位.
參考:
https://bugs.mysql.com/bug.php?spm=5176.100239.blogcont27649.4.tlI76p&id=72076
http://didrikdidrik.blogspot.co.uk/
CREATE TABLE `TestCase2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`aValue` decimal(19,2) NOT NULL,
`accuracyClassType_id` bigint(20) NOT NULL,
`productType_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `FKF58064BD791396` (`productType_id`),
KEY `FKF58064BDED5076` (`accuracyClassType_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
INSERT INTO `TestCase2` (`id`, `aValue`, `accuracyClassType_id`, `productType_id`)
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);
select * from Testcase2 order by aValue desc;
# as you can see mysql has added a fallback sorting because aValue is not unique, which is ok
# the first four id's of the results are: 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 4;
# as expected the result id's (based on the order by) are : 10, 9, 14, 8
select * from Testcase2 order by aValue desc limit 3;
# which surprisingly results in the following id's based on the order by: 10, 9, 8 ???????
# expecting id's: 10, 9, 14 (see query with limit 4)[19 Mar 2014 13:34] Miguel SolorzanoThank you for the bug report.
mysql 5.1 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.1 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 4;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 14 | 12.00 | 2 | 4 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
4 rows in set (0.00 sec)
mysql 5.6 > select * from Testcase2 order by aValue desc limit 3;
+----+--------+----------------------+----------------+
| id | aValue | accuracyClassType_id | productType_id |
+----+--------+----------------------+----------------+
| 10 | 20.00 | 2 | 3 |
| 9 | 16.00 | 2 | 3 |
| 8 | 12.00 | 2 | 3 |
+----+--------+----------------------+----------------+
3 rows in set (0.00 sec)
mysql 5.5 跟 5.6 兩個版本對排序的實現:
Mysql 5.5 willl
- allocate buffer space for 1000 rows
- scan the table, inserting each row into the buffer
- sort the buffer, this requires about 10.000 comparisons
- return the first 3 (or 4) rowsMysql
5.6 will
- allocate buffer space for 3 (or 4) rows
- scan the table, maintaining a collection of 3 (or 4) "winners" this requires about 2000 comparisons
- sort final set of "winners", this requires about 6 comparisons
- return the result
sql 標準裡對於重複值的排序欄位, 應該出哪行記錄是沒有定義的, 這就完全取決於程式碼的實現了.
我們的升級測試,可能會涉及到 5.5 與5.6 的結果對比, 這裡可能會引起疑問.
這是正常的. 如果確實需要嚴格的排序,實現5.6 跟5.5 同樣的顯示結果.
需要修改sql 語句, 在排序子句中加上 一個唯一欄位.
參考:
https://bugs.mysql.com/bug.php?spm=5176.100239.blogcont27649.4.tlI76p&id=72076
http://didrikdidrik.blogspot.co.uk/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-2133364/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於全問檢索按照日期的排序問題排序
- Oracle學習系列—關於字元數字混合排序和中文排序的問題Oracle字元排序
- 關於SQLServerDriver的問題SQLServer
- 關於 JavaMail 的問題JavaAI
- 關於session的問題Session
- 關於陣列的物件獲取及排序問題/小程式的多層頁面返回問題陣列物件排序
- 與堆和堆排序相關的問題排序
- 關於javascript的this指向問題JavaScript
- 關於跨域的問題跨域
- 關於bit code的問題
- 關於序列同步的問題
- 關於IP地址的問題
- 關於橋模式的問題模式
- 求救 關於parallel的問題Parallel
- 關於web start的問題Web
- 關於action的error問題Error
- 關於ADAPTER的問題APT
- 關於session的奇怪問題Session
- php關於session的問題PHPSession
- 關於diag程式的問題
- 關於SimpleJdonFrameworkTest的問題Framework
- 關於 Puerts 的效能問題
- 關於盒模型相關的問題模型
- 關於FastHashMap問題ASTHashMap
- 關於java記憶體訪問重排序的思考Java記憶體排序
- 關於PHP佇列的問題PHP佇列
- 關於 Laravel 分頁的問題?Laravel
- 關於css權值的問題CSS
- 關於搜尋地址的問題
- 關於PWA落地問題的思考
- 關於 API 合併的問題API
- 關於excelize庫的使用問題Excelize
- 關於scanf函式的問題函式
- 關於session leak的問題分析Session
- 關於oracle的監聽問題Oracle
- 關於partition table import的問題Import
- 關於資料共享的問題
- 關於mysql連線的問題MySql