Description:
The sort order of a view is incorrect under the condition below:
- The view is defined with "order by" clause
- The view is selected with a "where" clause
Here is a sample script which reproduces the behavior. See the attached file "mysql_bug.sql".
When the script is executed, the records should be sorted by column "sort_order",
but when a where clause is specified, the actual order is different as shown below.
mysql> select * from v1 where c1 = "a"
-> ;
+-----------+------------+------+---+
| table1_id | sort_order | c1 | x |
+-----------+------------+------+---+
| 7 | 6 | a | 0 | *** WRONG ***
| 4 | 7 | a | 0 | *** WRONG ***
| 1 | 2 | a | 0 | *** WRONG ***
+-----------+------------+------+---+
This behavior happened at:
- MySQL 5.6.17 / Windows7(64bit)
------------------------------------------------------------
CHECK LOG
------------------------------------------------------------
[MySQL 5.6.17 / Windows7(64bit)] => This behavior happened
mysql> show global variables like '%version%';
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 5.6.17 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.17-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Win64 |
+-------------------------+------------------------------+
7 rows in set (0.01 sec)
mysql>
mysql> drop view if exists v1;
Query OK, 0 rows affected (0.01 sec)
mysql> drop table if exists t1;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> create table t1 (
-> table1_id bigint not null auto_increment,
-> sort_order int(11),
-> c1 varchar(255),
-> primary key (`table1_id`)
-> ) engine=InnoDB default charset=utf8
-> ;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> create view v1 as
-> select t1.*, (select 0) x
-> from t1
-> order by sort_order
-> ;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert into t1 (sort_order, c1) values
-> (2, "a"),(9, "b"),(4, "c"),
-> (7, "a"),(5, "b"),(3, "c"),
-> (6, "a"),(1, "b"),(8, "c")
-> ;
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql>
mysql> select * from v1
-> ;
+-----------+------------+------+---+
| table1_id | sort_order | c1 | x |
+-----------+------------+------+---+
| 8 | 1 | b | 0 |
| 1 | 2 | a | 0 |
| 6 | 3 | c | 0 |
| 3 | 4 | c | 0 |
| 5 | 5 | b | 0 |
| 7 | 6 | a | 0 |
| 4 | 7 | a | 0 |
| 9 | 8 | c | 0 |
| 2 | 9 | b | 0 |
+-----------+------------+------+---+
9 rows in set (0.00 sec)
mysql>
mysql> select * from v1 where c1 = "a"
-> ;
+-----------+------------+------+---+
| table1_id | sort_order | c1 | x |
+-----------+------------+------+---+
| 7 | 6 | a | 0 | *** WRONG ***
| 4 | 7 | a | 0 | *** WRONG ***
| 1 | 2 | a | 0 | *** WRONG ***
+-----------+------------+------+---+
3 rows in set (0.00 sec)
mysql>
------------------------------------------------------------
[MySQL 5.6.16 / Windows7(64bit)] => This behavior not happened
mysql> show global variables like '%version%';
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 5.6.16 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.16-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Win64 |
+-------------------------+------------------------------+
7 rows in set (0.00 sec)
mysql>
mysql> drop view if exists v1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> drop table if exists t1;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
mysql> create table t1 (
-> table1_id bigint not null auto_increment,
-> sort_order int(11),
-> c1 varchar(255),
-> primary key (`table1_id`)
-> ) engine=InnoDB default charset=utf8
-> ;
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql> create view v1 as
-> select t1.*, (select 0) x
-> from t1
-> order by sort_order
-> ;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert into t1 (sort_order, c1) values
-> (2, "a"),(9, "b"),(4, "c"),
-> (7, "a"),(5, "b"),(3, "c"),
-> (6, "a"),(1, "b"),(8, "c")
-> ;
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql>
mysql> select * from v1
-> ;
+-----------+------------+------+---+
| table1_id | sort_order | c1 | x |
+-----------+------------+------+---+
| 8 | 1 | b | 0 |
| 1 | 2 | a | 0 |
| 6 | 3 | c | 0 |
| 3 | 4 | c | 0 |
| 5 | 5 | b | 0 |
| 7 | 6 | a | 0 |
| 4 | 7 | a | 0 |
| 9 | 8 | c | 0 |
| 2 | 9 | b | 0 |
+-----------+------------+------+---+
9 rows in set (0.00 sec)
mysql>
mysql> select * from v1 where c1 = "a"
-> ;
+-----------+------------+------+---+
| table1_id | sort_order | c1 | x |
+-----------+------------+------+---+
| 1 | 2 | a | 0 |
| 7 | 6 | a | 0 |
| 4 | 7 | a | 0 |
+-----------+------------+------+---+
3 rows in set (0.00 sec)
mysql>
------------------------------------------------------------
How to repeat:
Please check attached file.(mysql_bug.sql)