今天吓坏一个很诡异的问题,关于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
热点新闻
Atitit .linux 取走root 密码q99
MySQL中RESET SLAVE和RESET MASTER的区别
TokuDB存储引擎
MySQL数据库下用户及用户权限配有
1.Mariadb(mysql)基本操作
MySQL关于exists的一个bug
干货~多线程下1分钟已完成1000万条数据插进到数据…
MySQL充分利用两张列于数据的同步
有关binlog的那点事(mysql5.7.13)
年轻熟妇人妻肉欲系列|输了让同学玩全部位置的游戏
MySQL的20条基本优化 赞参考资料
MYSQL(三)
MySQL外键与外键关系指明(最简单易懂)
内嵌CSV格式的数据
缓慢而有力的一下又一下/超级乱婬伦怀孕小说全集
关于我们 - 广告合作 - 联系我们 - 免责声明 - 网站地图 - 投诉建议 - 在线投稿
©CopyRight 2008-2020 caicaipc.com Inc All Rights Reserved.
菜菜电脑网 版权所有