MariaDB 10.1 Order By + Limit不一致

时间:2016-11-01 08:13:21

标签: mysql sql mariadb

当通过查询运行订单且对MariaDB 10.1.18有限制时,我收到了错误的订单。

在没有LIMIT语句的情况下观察查询:

select advert_id, published, id from vacancies order by published asc;

+-----------+-----------+----+
| advert_id | published | id |
+-----------+-----------+----+
|    328377 |         0 | 70 |
|    328844 |         0 | 80 |
|    325263 |         0 | 41 |
|    325774 |         0 | 40 |
|    325775 |         0 | 39 |
|    325929 |         0 | 38 |
|    325885 |         0 | 37 |
|    325901 |         0 | 36 |
|    325920 |         0 | 35 |
|    325917 |         0 | 34 |
|    325922 |         0 | 33 |
|    325889 |         0 | 32 |
|    325927 |         0 | 31 |
|    325238 |         0 | 43 |
|    325244 |         0 | 45 |
|    328365 |         0 | 71 |
|    328446 |         0 | 72 |
|    328362 |         0 | 68 |
|    323602 |         0 | 55 |
|    324250 |         0 | 54 |
|    324254 |         0 | 53 |
|    324911 |         0 | 52 |

使用LIMIT声明:

select advert_id, published, id from vacancies order by published asc limit 10;

+-----------+-----------+----+
| advert_id | published | id |
+-----------+-----------+----+
|    327830 |         0 |  1 |
|    326865 |         0 | 18 |
|    327328 |         0 |  9 |
|    326877 |         0 | 16 |
|    326783 |         0 | 21 |
|    326779 |         0 | 17 |
|    326774 |         0 | 15 |
|    326864 |         0 | 20 |
|    326788 |         0 | 14 |
|    326767 |         0 | 19 |
+-----------+-----------+----+

发布的订单在两个查询中都有所不同。

为了进行比较,我在MariaDB 5.5.50上运行了相同的查询,发现+ limit的顺序正确返回与查询顺序相同的结果。因此,根据我的理解,这个问题是特定于MariaDB的,并且只存在于较新的版本中。

此外,我还运行了相同的查询,但是在具有许多不同值的varchar字段上进行排序,在这种情况下,顺序是正确的。所以我认为这个问题只适用于对具有大量相同值的字段进行限制的排序。

有没有人知道是否有办法解决这个问题?也许是MariaDB中的一个设置?

供参考:

表格结构:

+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| id               | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| advert_id        | int(11)          | YES  |     | NULL    |                |
| published        | tinyint(1)       | NO   |     | 0       |                |
| (other fields omitted)

解释查询:

explain select advert_id, published, id from vacancies order by published asc;
+------+-------------+-----------+------+---------------+------+---------+------+------+----------------+
| id   | select_type | table     | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+------+-------------+-----------+------+---------------+------+---------+------+------+----------------+
|    1 | SIMPLE      | vacancies | ALL  | NULL          | NULL | NULL    | NULL |   52 | Using filesort |
+------+-------------+-----------+------+---------------+------+---------+------+------+----------------+

explain select advert_id, published, id from vacancies order by published asc limit 10;
+------+-------------+-----------+------+---------------+------+---------+------+------+----------------+
| id   | select_type | table     | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+------+-------------+-----------+------+---------------+------+---------+------+------+----------------+
|    1 | SIMPLE      | vacancies | ALL  | NULL          | NULL | NULL    | NULL |   52 | Using filesort |
+------+-------------+-----------+------+---------------+------+---------+------+------+----------------+

按问题排序的版本:

mysql  Ver 15.1 Distrib 10.1.18-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

没有按订单排序的版本:

mysql  Ver 15.1 Distrib 5.5.50-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

1 个答案:

答案 0 :(得分:3)

您没有指定要获取的前十行。因为有很多行published等于0,所以MariaDB可以自由选择其中一些。如果您想要特定订单,请尝试:

SELECT advert_id, published, id FROM vacancies ORDER BY published asc, id LIMIT 10;