MySQL關(guān)于exists的一個(gè)bug
今天碰到一個(gè)關(guān)于exists很奇怪的問題
第一個(gè)語句如下:
SELECT count(1) FROM APPLY t WHERE EXISTS ( SELECT r.APPLY_ID FROM RECORD r WHERE t.APPLY_ID = r.APPLY_ID );
產(chǎn)生的結(jié)果是:89584
第二個(gè)語句如下:
SELECT count(1) FROM APPLY t WHERE EXISTS ( SELECT max(r.FINISH_TIME) FROM RECORD r WHERE t.APPLY_ID = r.APPLY_ID );
產(chǎn)生的結(jié)果是:432382
確實(shí)相當(dāng)奇怪,對(duì)于exist子句來說,其判斷的是子查詢的值是否存在,也就是說,列名,和對(duì)列名求最大值沒什么區(qū)別啊。
包括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會(huì)自動(dòng)忽略到SELECT的列表。
后來在自己的環(huán)境測(cè)試了一下,確實(shí)是MySQL的一個(gè)bug
測(cè)試環(huán)境: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的列不存在,但是第二條語句還是當(dāng)做TRUE來處理了。
也確認(rèn)了下兩條語句的執(zhí)行計(jì)劃和改寫后的SQL
第一個(gè)語句
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 | +---------+------+-------------------------------------------------------------------+
第二個(gè)語句
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)
執(zhí)行計(jì)劃及改寫后的SQL確實(shí)有所不同,看來,確實(shí)是MySQL的一個(gè)bug了。
于是,給官方提了個(gè)bug
http://bugs.mysql.com/bug.php?id=82562
總結(jié)
建議寫exists語句時(shí),子查詢中直接用*,而不用對(duì)列進(jìn)行任何函數(shù)操作,避免碰到官方bug,
事實(shí)上,對(duì)于abs,floor函數(shù)又沒問題
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關(guān)于exists的一個(gè)bug ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解MySQL如何實(shí)現(xiàn)數(shù)據(jù)批量更新
最近需要批量更新大量數(shù)據(jù),習(xí)慣了寫sql,所以還是用sql來實(shí)現(xiàn),下面這篇文章主要給大家總結(jié)介紹了關(guān)于MySQL批量更新的方式,需要的朋友可以參考下2023-10-10mysql刪除關(guān)聯(lián)表的實(shí)操方法
在本篇內(nèi)容里我們給大家整理了關(guān)于mysql刪除關(guān)聯(lián)表的實(shí)操方法以及相關(guān)SQL語句,需要的朋友們學(xué)習(xí)下吧。2019-05-05一個(gè) 20 秒 SQL 慢查詢優(yōu)化處理方案
這篇文章主要分享一個(gè) 20 秒 SQL 慢查詢優(yōu)化的經(jīng)歷與處理方案,頁面無法正確獲取數(shù)據(jù),經(jīng)排查原來是接口調(diào)用超時(shí),而最后發(fā)現(xiàn)是因?yàn)镾QL查詢長達(dá)到20多秒而導(dǎo)致了問題的發(fā)生,下面來看問題具體介紹吧2022-01-01基于ubuntu中使用mysql實(shí)現(xiàn)opensips用戶認(rèn)證的解決方法
本篇文章小編為大家介紹,基于ubuntu中使用mysql實(shí)現(xiàn)opensips用戶認(rèn)證的解決方法。需要的朋友參考下2013-04-04MySQL中配置文件my.cnf因權(quán)限問題導(dǎo)致無法啟動(dòng)的解決方法
這篇文章主要給大家介紹了關(guān)于MySQL中配置文件my.cnf因權(quán)限問題導(dǎo)致無法啟動(dòng)的解決方法,該無法啟動(dòng)的錯(cuò)誤提示代碼是:World-writable config file '/etc/my.cnf' is ignored,文中給出了詳細(xì)的解決方法,需要的朋友們下面來一起看看吧。2017-06-06