[MDEV-10560] Different results when change select in simple query Created: 2016-08-15  Updated: 2016-08-16  Resolved: 2016-08-16

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.1.16
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: masoud sabaghi Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: upstream

Attachments: File employee.sql.gz     Zip Archive employee_structure.zip    

 Description   

Hi
I run two query
First
SELECT id FROM employee ORDER BY first_name DESC LIMIT 1,1
Result Id : 61394
Second
SELECT * FROM employee ORDER BY first_name DESC LIMIT 1,1
Result Id : 128257

Also when i run this queries
SELECT id FROM employee ORDER BY first_name DESC LIMIT 20 OFFSET 0
SELECT id FROM employee ORDER BY first_name DESC LIMIT 20 OFFSET 20
Id 80129 exists in both query (id is primary key)

The table is exist in attachment.



 Comments   
Comment by Daniel Black [ 2016-08-16 ]

which server version are you running?

Adding first_name as an additional key would also help speed up these queries.

edit: removed PK suggestion - it was at end of sql.

Comment by masoud sabaghi [ 2016-08-16 ]

Version 10.1.16 but id had this problme on mysql 5.6 too
I fixed it with second order sort condition. Also It will fix with index on first_name.
But i think default behavior it isn't correct
I get backup with phpmyadmin, i don't know why PK is at end of sql.
Separate table structure and data added
employee_structure.zip

Comment by Elena Stepanova [ 2016-08-16 ]

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

If your next argument is "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:43:09 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.