电脑技术网——专业手机电脑知识平台,关注科技、手机、电脑、智能硬件
MySQLMSSQLAccessOracle

MySQL关于exists的一个bug

2021-01-22 12:00:58 出处:[ 菜菜电脑网 ] 人气:次阅读

今天吓坏一个很诡异的问题,关于exists的,

第一个语句如下:

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

产生的结果是:89584

 

第二个语句如下:

SELECT    count(1)FROM    APPLY tWHERE    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

关于我们 - 广告合作 - 联系我们 - 免责声明 - 网站地图 - 投诉建议 - 在线投稿

©CopyRight 2008-2020 caicaipc.com Inc All Rights Reserved.
菜菜电脑网 版权所有