MySQL关于exists的一个bug

今赶上一个大意外之题材,关于exists的,

第一独告知词如下:

SELECT
    count(1)
FROM
    APPLY t
WHERE
    EXISTS (
        SELECT
            r.APPLY_ID
        FROM
            RECORD r
        WHERE
            t.APPLY_ID = r.APPLY_ID
    );

有的结果是:89584

 

第二单告知句如下:

SELECT
    count(1)
FROM
    APPLY t
WHERE
    EXISTS (
        SELECT
            max(r.FINISH_TIME)
        FROM
            RECORD r
        WHERE
            t.APPLY_ID = r.APPLY_ID
    );

出的结果是:432382

 

当真相当奇怪,对于exist子句来说,其判断的是子查询的值是否存在,也就是说,列名,和对列名求最要命价值没什么分别啊。

概括MySQL官方文档中吗事关

Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT list in such a subquery, so it makes no difference.

不经意就是MySQL会自动忽略到SELECT的列表。

 

后来以祥和之条件测试了转,确实是MySQL的一个bug

测试环境:MySQL 5.6.31,5.7.14

mysql> create table t3(id int,t datetime);
Query OK, 0 rows affected (0.44 sec)

mysql> insert into t3 values(1,'20160812');
Query OK, 1 row affected (0.16 sec)

mysql> select 1 from dual where  exists (select id from t3 where id=2);
Empty set (0.15 sec)

mysql> select 1 from dual where  exists (select max(id) from t3 where id=2);
+---+
| 1 |
+---+
| 1 |

老肯定,id等于2的排非在,但是第二条语句还是作为TRUE来处理了。

 

为肯定了下零星长达告句之行计划同改写后的SQL

首先单话

mysql> EXPLAIN EXTENDED select 1 from dual where  exists (select id from t3 where id=2);
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra            |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
|  1 | PRIMARY     | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Impossible WHERE |
|  2 | SUBQUERY    | t3    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    1 |   100.00 | Using where      |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> show warnings;
+---------+------+-------------------------------------------------------------------+
| Level   | Code | Message                                                           |
+---------+------+-------------------------------------------------------------------+
| Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. |
| Note    | 1003 | /* select#1 */ select 1 AS `1` from DUAL  where 0                 |
+---------+------+-------------------------------------------------------------------+

 

亚只话

mysql> EXPLAIN EXTENDED select 1 from dual where  exists (select max(id) from t3 where id=2);
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
|  1 | PRIMARY     | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
|  2 | SUBQUERY    | t3    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    1 |   100.00 | Using where    |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> show warnings;
+---------+------+-------------------------------------------------------------------+
| Level   | Code | Message                                                           |
+---------+------+-------------------------------------------------------------------+
| Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. |
| Note    | 1003 | /* select#1 */ select 1 AS `1` from DUAL  where 1                 |
+---------+------+-------------------------------------------------------------------+
2 rows in set (0.00 sec)

 

履行计划及改写后底SQL确实有所不同,看来,确实是MySQL的一个bug了。

乃,给合法提了个bug

http://bugs.mysql.com/bug.php?id=82562

 

总结

建议写exists语句时,子查询中一直用*,而无用对列进行任何函数操作,避免碰到官方bug,

其实,对于abs,floor函数又无问题

mysql> select 1 from dual where  exists (select abs(id) from t3 where id=2);
Empty set (0.07 sec)

mysql> select 1 from dual where  exists (select floor(id) from t3 where id=2);
Empty set (0.00 sec)

 

后续

是真的不是一个bug,这个是符合SQL标准的。

好参考官方的恢复

http://bugs.mysql.com/bug.php?id=82562

网站地图xml地图