MySQL关于exists的一个bug
程序员文章站
2023-12-19 13:53:52
今天碰到一个关于exists很奇怪的问题
第一个语句如下:
select
count(1)
from
apply t
where
exists...
今天碰到一个关于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
总结
建议写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)
以上所述是小编给大家介绍的mysql关于exists的一个bug ,希望对大家有所帮助