Issues

Select view

Select search mode

 
27 of 27

Sort bug in special case

Won't Do

Description

We found a bug in a usual query, which produces results in a wrong order.

The query is like this:

SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0;

Where we get different results in different Server Version.

In version 5.7.20-19 we get:

mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.25-28, for debian-linux-gnu (x86_64) using 6.3 Connection id: 11 Current database: sort_bug Current user: root@192.168.150.1 SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256 Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.20-19 Percona Server (GPL), Release '19', Revision '3c5d3e5d53c' Protocol version: 10 Connection: 192.168.150.5 via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 TCP port: 3306 Uptime: 45 min 28 sec Threads: 1 Questions: 641 Slow queries: 0 Opens: 124 Flush tables: 1 Open tables: 106 Queries per second avg: 0.234 -------------- mysql> SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555292700 | 2004823834659 | | 555292699 | 2087842381870 | | 555292698 | 2168547836040 | | 555292697 | NULL | | 555292696 | NULL | +-----------+-------------------+ 5 rows in set (1.67 sec)

(the correct result)

Beginning from 5.7.21 up to the current latest (5.7.29), we get this result:

mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.25-28, for debian-linux-gnu (x86_64) using 6.3 Connection id: 13 Current database: sort_bug Current user: root@192.168.150.1 SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256 Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.21-21 Percona Server (GPL), Release '21', Revision '2a37e4e' Protocol version: 10 Connection: 192.168.150.2 via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 TCP port: 3306 Uptime: 8 hours 31 min 46 sec Threads: 1 Questions: 679 Slow queries: 0 Opens: 152 Flush tables: 1 Open tables: 122 Queries per second avg: 0.022 -------------- mysql> SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555000000 | 671263031366 | | 555000001 | 709922095177 | | 555000002 | 709949980745 | | 555000003 | 710472335433 | | 555000004 | 650890018872 | +-----------+-------------------+ 5 rows in set (0.86 sec)
mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.25-28, for debian-linux-gnu (x86_64) using 6.3 Connection id: 15 Current database: sort_bug Current user: root@192.168.150.1 SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256 Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.29-32 Percona Server (GPL), Release 32, Revision 56bce88 Protocol version: 10 Connection: 192.168.150.6 via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 TCP port: 3306 Uptime: 43 min 51 sec Threads: 1 Questions: 772 Slow queries: 0 Opens: 139 Flush tables: 1 Open tables: 111 Queries per second avg: 0.293 -------------- mysql> SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555000000 | 671263031366 | | 555000001 | 709922095177 | | 555000002 | 709949980745 | | 555000003 | 710472335433 | | 555000004 | 650890018872 | +-----------+-------------------+ 5 rows in set (0.94 sec)

Take care of the wrong sort order while using the DESC keyword.

Even more we get the correct order, when leaving out the external_order_id column and confusing results with other select sets:

mysql> SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555000000 | 671263031366 | | 555000001 | 709922095177 | | 555000002 | 709949980745 | | 555000003 | 710472335433 | | 555000004 | 650890018872 | +-----------+-------------------+ 5 rows in set (0.94 sec) mysql> SELECT DISTINCT t1.order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+ | order_id | +-----------+ | 555292700 | | 555292699 | | 555292698 | | 555292697 | | 555292696 | +-----------+ 5 rows in set (0.88 sec) mysql> SELECT DISTINCT * FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-------------------+-----------+-------------------+-------+ | order_revision_id | order_id | external_order_id | state | +-------------------+-----------+-------------------+-------+ | 405 | 555000404 | 701250011200 | NEW | | 720 | 555000719 | 1000474738801 | NEW | | 737 | 555000736 | 1001633513585 | NEW | | 3145 | 555001396 | 1000474738801 | NEW | | 3146 | 555001397 | 1001633513585 | NEW | +-------------------+-----------+-------------------+-------+ 5 rows in set (1.15 sec) mysql> SELECT DISTINCT t1.* FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+-------------------+ | order_id | order_revision_id | external_order_id | +-----------+-------------------+-------------------+ | 555000000 | 17720 | 671263031366 | | 555000001 | 6639 | 709922095177 | | 555000002 | 6640 | 709949980745 | | 555000003 | 6641 | 710472335433 | | 555000004 | 17721 | 650890018872 | +-----------+-------------------+-------------------+ 5 rows in set (0.95 sec)

Please investigate, whats the reason ...

Thanks in advance!

Environment

Docker image and plain package installation on Ubuntu 16.

Attachments

1
  • 03 Apr 2020, 05:04 PM

Smart Checklist

Details

Assignee

Reporter

Labels

Affects versions

Priority

Smart Checklist

Created April 3, 2020 at 5:03 PM
Updated March 4, 2025 at 9:09 PM
Resolved March 4, 2025 at 9:09 PM

Activity

Show:

Julia Vural March 4, 2025 at 9:09 PM

It appears that this issue is no longer being worked on, so we are closing it for housekeeping purposes. If you believe the issue still exists, please open a new ticket after confirming it's present in the latest release.

Lalit Choudhary April 14, 2020 at 2:28 PM

Lalit Choudhary April 14, 2020 at 1:50 PM
Edited

Testing further I found that issue originally reported for 5.7 old vs New version, Can also visible between 5.7.29  and 8.0.19 version.

 
Percona server:

Server version: 5.7.29-32-log Percona Server SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555000000 | 671263031366 | | 555000001 | 709922095177 | | 555000002 | 709949980745 | | 555000003 | 710472335433 | | 555000004 | 650890018872 | +-----------+-------------------+ 5 rows in set (2.21 sec) Server version: 8.0.19-10 Percona Server SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555292700 | 2004823834659 | | 555292699 | 2087842381870 | | 555292698 | 2168547836040 | | 555292697 | NULL | | 555292696 | NULL | +-----------+-------------------+ 5 rows in set (0.00 sec)

Upstream MySQL:

mysql 5.7.29 SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555000000 | 671263031366 | | 555000001 | 709922095177 | | 555000002 | 709949980745 | | 555000003 | 710472335433 | | 555000004 | 650890018872 | +-----------+-------------------+ 5 rows in set (1.22 sec) MySQL 8.0.19 SELECT DISTINCT t1.order_id, t1.external_order_id FROM t1 INNER JOIN t2 USING (order_revision_id) ORDER BY t1.order_id DESC LIMIT 5 OFFSET 0; +-----------+-------------------+ | order_id | external_order_id | +-----------+-------------------+ | 555292700 | 2004823834659 | | 555292699 | 2087842381870 | | 555292698 | 2168547836040 | | 555292697 | NULL | | 555292696 | NULL | +-----------+-------------------+ 5 rows in set (0.00 sec)

So Here we have 2 issues when using order by with limit,
1. Inconsistent output between versions
2. Order By sorting is wrong

 

 

SteffenS April 14, 2020 at 10:49 AM

Thank you!

Lalit Choudhary April 14, 2020 at 10:46 AM

Thank you for your reply.

for consistent output for both 5.7.20 and 5.7.29 was referring to the upstream mysql versions test. 

Regarding "ORDER BY t1.order_id DESC"   yes, results for the  order_id column is  ASC and not DESC.

I will review it again and re-open it.