博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL关于exists的一个bug
阅读量:7236 次
发布时间:2019-06-29

本文共 4393 字,大约阅读时间需要 14 分钟。

今天碰到一个很奇怪的问题,关于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

 

总结

建议写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://tnlfm.baihongyu.com/

你可能感兴趣的文章
手把手教你搭建智能合约测试环境、开发、编译、部署以及如何通过JS调用合约方法...
查看>>
Flex 布局教程
查看>>
固定宽度布局开发WebApp如何实现多终端下自适应?
查看>>
伟大的Scrum团队的特征
查看>>
2019春节防坑指南之抢票陷阱(文末有彩蛋)
查看>>
深入node.js-浏览器缓存机制
查看>>
解决IOS中input失焦后,页面上移,点击不了问题
查看>>
【C】 23_#error 和 #line 使用分析
查看>>
浅谈 Java 10 ,你可能不知道的五件事
查看>>
IT 人能在一线城市里生活一辈子吗?
查看>>
VUE的总结(1)
查看>>
【PWA学习与实践】(5)在Web中进行服务端消息推送
查看>>
WebAssembly完全入门——了解wasm的前世今身
查看>>
SAP CRM和C4C数据同步的两种方式概述:SAP PI和HCI
查看>>
SAP Cloud for Customer Extensibility的设计与实现
查看>>
Nacos 发布0.3.0版本,迄今为止最好看的版本
查看>>
如何愉快的在PhpStorm中进行PHPUnit单元测试和Xdebug断点调试?
查看>>
Pod Preset玩转K8S容器时区自动配置
查看>>
PHP多进程初探 --- 进程间通信二三事
查看>>
[源码阅读]解析Anime(JS动画库)核心(1)
查看>>