欢迎来到代码驿站!

Mysql

当前位置:首页 > 数据库 > Mysql

MySQL关于exists的一个bug

时间:2021-05-01 09:32:23|栏目:Mysql|点击:

今天碰到一个关于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)

上一篇:asp采用ODBC接口访问MySQL的方法

栏    目:Mysql

下一篇:mysql最大连接数设置技巧总结

本文标题:MySQL关于exists的一个bug

本文地址:http://www.codeinn.net/misctech/112273.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有