UPDATE ... WHERE ... ORDER BY... always does a filesort when not required

Bug #1324405 reported by Muhammad Irfan
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MySQL Server
Unknown
Unknown
Percona Server moved to https://jira.percona.com/projects/PS
Status tracked in 5.7
5.1
Won't Fix
Medium
Unassigned
5.5
Triaged
Medium
Unassigned
5.6
Triaged
Medium
Unassigned
5.7
Triaged
Medium
Unassigned
Percona XtraDB Cluster moved to https://jira.percona.com/projects/PXC
Status tracked in 5.6
5.5
Confirmed
Undecided
Unassigned
5.6
Confirmed
Undecided
Unassigned

Bug Description

C:\Program Files\MySQL\MySQL Server 5.5\bin>mysql -uroot -proot -P3312 test
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE TABLE update_test (a INT AUTO_INCREMENT PRIMARY KEY, b INT, c INT,
 INDEX (b,c));
Query OK, 0 rows affected (1.20 sec)

mysql> INSERT INTO update_test (b, c) VALUES (1, 1), (1, 2), (1,3), (2, 2), (2,
3), (2,4), (2, 5), (5, 5), (6,6), (7,7);
Query OK, 10 rows affected (0.19 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql>
mysql> -- no filesort
mysql> SELECT * FROM update_test WHERE b = 1 ORDER BY c LIMIT 2;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
+---+------+------+
2 rows in set (0.13 sec)

mysql> SHOW SESSION STATUS LIKE 'Sort%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 0 |
| Sort_rows | 0 |
| Sort_scan | 0 |
+-------------------+-------+
4 rows in set (0.08 sec)

mysql>
mysql> -- does an unneeded filesort
mysql> UPDATE update_test SET a = a + 10 WHERE b = 1 ORDER BY c LIMIT 2;
Query OK, 2 rows affected (0.11 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> SHOW SESSION STATUS LIKE 'Sort%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 1 |
| Sort_rows | 2 |
| Sort_scan | 0 |
+-------------------+-------+
4 rows in set (0.00 sec)

tags: added: i42464
Revision history for this message
Muhammad Irfan (muhammad-irfan) wrote :

This bug is originally reported for PXC version PXC 5.5.34.

Revision history for this message
Valerii Kravchuk (valerii-kravchuk) wrote :
Download full text (3.4 KiB)

This is easy to confirm with PS 5.6.17:

openxs@ao756:~/dbs/p5.6$ mysql -uroot -proot test
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.17-65.0-587.saucy (Ubuntu)

Copyright (c) 2009-2014 Percona LLC and/or its affiliates
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE TABLE update_test (a INT AUTO_INCREMENT PRIMARY KEY, b INT, c INT,
    -> INDEX (b,c));
Query OK, 0 rows affected (0,93 sec)

mysql> INSERT INTO update_test (b, c) VALUES (1, 1), (1, 2), (1,3), (2, 2), (2,
    -> 3), (2,4), (2, 5), (5, 5), (6,6), (7,7);
Query OK, 10 rows affected (0,08 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM update_test WHERE b = 1 ORDER BY c LIMIT 2;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
+---+------+------+
2 rows in set (0,06 sec)

mysql> SHOW SESSION STATUS LIKE 'Sort%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 0 |
| Sort_rows | 0 |
| Sort_scan | 0 |
+-------------------+-------+
4 rows in set (0,00 sec)

mysql> explain SELECT * FROM update_test WHERE b = 1 ORDER BY c LIMIT 2;
+----+-------------+-------------+------+---------------+------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+---------------+------+---------+-------+------+--------------------------+
| 1 | SIMPLE | update_test | ref | b | b | 5 | const | 3 | Using where; Using index |
+----+-------------+-------------+------+---------------+------+---------+-------+------+--------------------------+
1 row in set (0,02 sec)

mysql> UPDATE update_test SET a = a + 10 WHERE b = 1 ORDER BY c LIMIT 2;
Query OK, 2 rows affected (0,07 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> SHOW SESSION STATUS LIKE 'Sort%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 1 |
| Sort_rows | 2 |
| Sort_scan | 0 |
+-------------------+-------+
4 rows in set (0,00 sec)

mysql> explain UPDATE update_test SET a = a + 10 WHERE b = 1 ORDER BY c LIMIT 2;
+----+-------------+-------------+-------+---------------+------+---------+-------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+---------------+------+---------+-------+--...

Read more...

tags: added: upstream
Revision history for this message
Muhammad Irfan (muhammad-irfan) wrote :
Download full text (3.9 KiB)

Confirmed on 5.6.15-56-log Percona XtraDB Cluster

mysql> show global variables like '%version%';
+-------------------------+----------------------------------------------------------------------------+
| Variable_name | Value |
+-------------------------+----------------------------------------------------------------------------+
| innodb_version | 5.6.15-rel63.0 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.15-56-log |
| version_comment | Percona XtraDB Cluster (GPL), Release 25.5, Revision 759, wsrep_25.5.r4061 |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
+-------------------------+----------------------------------------------------------------------------+

mysql> use test
Database changed

mysql> CREATE TABLE update_test (a INT AUTO_INCREMENT PRIMARY KEY, b INT, c INT, INDEX (b,c));
Query OK, 0 rows affected (0.16 sec)

mysql> INSERT INTO update_test (b, c) VALUES (1, 1), (1, 2), (1,3), (2, 2), (2,3), (2,4), (2, 5), (5, 5), (6,6), (7,7);
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM update_test WHERE b = 1 ORDER BY c LIMIT 2;
+---+------+------+
| a | b | c |
+---+------+------+
| 2 | 1 | 1 |
| 4 | 1 | 2 |
+---+------+------+
2 rows in set (0.03 sec)

mysql> SHOW SESSION STATUS LIKE 'Sort%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 0 |
| Sort_rows | 0 |
| Sort_scan | 0 |
+-------------------+-------+
4 rows in set (0.00 sec)

mysql> explain SELECT * FROM update_test WHERE b = 1 ORDER BY c LIMIT 2;
+----+-------------+-------------+------+---------------+------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+---------------+------+---------+-------+------+--------------------------+
| 1 | SIMPLE | update_test | ref | b | b | 5 | const | 3 | Using where; Using index |
+----+-------------+-------------+------+---------------+------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

mysql> UPDATE update_test SET a = a + 13 WHERE b = 1 ORDER BY c LIMIT 2;
Query OK, 2 rows affected (0.69 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> SHOW SESSION STATUS LIKE 'Sort%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
| Sort_range | 1 |
| Sort_rows | 2 |
| Sort...

Read more...

Revision history for this message
Valerii Kravchuk (valerii-kravchuk) wrote :

See http://bugs.mysql.com/bug.php?id=72815 - new upstream bug created for this.

Revision history for this message
Shahriyar Rzayev (rzayev-sehriyar) wrote :

Percona now uses JIRA for bug reports so this bug report is migrated to: https://jira.percona.com/browse/PXC-1686

Revision history for this message
Shahriyar Rzayev (rzayev-sehriyar) wrote :

Percona now uses JIRA for bug reports so this bug report is migrated to: https://jira.percona.com/browse/PS-1505

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.