MySQL關於exists的一個bug。本站提示廣大學習愛好者:(MySQL關於exists的一個bug)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL關於exists的一個bug正文
明天碰著一個關於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)
以上所述是小編給年夜家引見的MySQL關於exists的一個bug ,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!