[MDEV-10733] record appear mutiply time in pagination query order by column with same value Created: 2016-09-03  Updated: 2016-09-03  Resolved: 2016-09-03

Status: Closed
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Affects Version/s: 10.1.17
Fix Version/s: N/A

Type: Bug Priority: Critical
Reporter: Jarod Liu Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

CentOS 7 64bit



 Description   

with mariadb 10.1, the record with id 8 appear twice

MariaDB [test]> DROP TABLE random_order;
Query OK, 0 rows affected, 1 warning (0.01 sec)
 
MariaDB [test]> CREATE TABLE `random_order` ( `Id` INT NOT NULL AUTO_INCREMENT , `Name` VARCHAR(8) NOT NULL , PRIMARY KEY (`Id`)) ENGINE = InnoDB;
Query OK, 0 rows affected (0.04 sec)
 
MariaDB [test]> INSERT INTO `random_order` (`Id`, `Name`) VALUES (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (N
ULL, 'a');
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 0,2;
+----+------+
| Id | Name |
+----+------+
|  8 | a    |
|  2 | a    |
+----+------+
2 rows in set (0.00 sec)
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 2,2;
+----+------+
| Id | Name |
+----+------+
|  3 | a    |
|  4 | a    |
+----+------+
2 rows in set (0.00 sec)
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 4,2;
+----+------+
| Id | Name |
+----+------+
|  5 | a    |
|  6 | a    |
+----+------+
2 rows in set (0.00 sec)
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 6,2;
+----+------+
| Id | Name |
+----+------+
|  7 | a    |
|  8 | a    |
+----+------+
2 rows in set (0.00 sec)

and mariadb 5.5 works as expected:

MariaDB [test]> DROP TABLE random_order;
Query OK, 0 rows affected (0.02 sec)
 
MariaDB [test]> CREATE TABLE `random_order` ( `Id` INT NOT NULL AUTO_INCREMENT , `Name` VARCHAR(8) NOT NULL , PRIMARY KEY (`Id`)) ENGINE = InnoDB;
Query OK, 0 rows affected (0.06 sec)
 
MariaDB [test]> INSERT INTO `random_order` (`Id`, `Name`) VALUES (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (NULL, 'a'), (N
ULL, 'a');
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 0,2;
+----+------+
| Id | Name |
+----+------+
|  1 | a    |
|  2 | a    |
+----+------+
2 rows in set (0.00 sec)
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 2,2;
+----+------+
| Id | Name |
+----+------+
|  3 | a    |
|  4 | a    |
+----+------+
2 rows in set (0.00 sec)
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 4,2;
+----+------+
| Id | Name |
+----+------+
|  5 | a    |
|  6 | a    |
+----+------+
2 rows in set (0.00 sec)
 
MariaDB [test]> SELECT * FROM `random_order` ORDER BY Name LIMIT 6,2;
+----+------+
| Id | Name |
+----+------+
|  7 | a    |
|  8 | a    |
+----+------+
2 rows in set (0.00 sec)



 Comments   
Comment by Elena Stepanova [ 2016-09-03 ]

You are ordering the result set by a non-unique column. You can only expect that if Name_1 < Name_2, then all rows with Name_1 will come earlier (in case of ASC) or later (in case of DESC) than all rows with Name_2. There is nothing that guarantees any specific order inside a group with the same Name. Technically, you can even get different results running the same query, e.g. one with LIMIT 0,2, although in practice it rarely happens; and of course, the next query knows nothing about the previous query.

It used to work on 5.5 – yes, it might well be true, but it worked by pure luck, the behavior has never been defined and guaranteed.

See also https://bugs.mysql.com/bug.php?id=69732 for longer discussion.

Generated at Thu Feb 08 07:44:30 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.