国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL關于exists的一個bug

2024-07-24 13:10:18
字體:
來源:轉載
供稿:網友

今天碰到一個關于exists很奇怪的問題

第一個語句如下:

SELECTcount(1)FROMAPPLY tWHEREEXISTS (SELECTr.APPLY_IDFROMRECORD rWHEREt.APPLY_ID = r.APPLY_ID);

產生的結果是:89584

第二個語句如下:

SELECTcount(1)FROMAPPLY tWHEREEXISTS (SELECTmax(r.FINISH_TIME)FROMRECORD rWHEREt.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 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到MYSQL教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会同县| 若羌县| 天门市| 隆德县| 进贤县| 军事| 株洲市| 达日县| 孟津县| 闸北区| 白玉县| 凭祥市| 牙克石市| 胶南市| 德安县| 长治县| 丹江口市| 汪清县| 商洛市| 社会| 万年县| 米林县| 黄大仙区| 汉川市| 池州市| 怀化市| 苍南县| 濮阳市| 莆田市| 通州市| 蒙城县| 普兰县| 南宁市| 富顺县| 长丰县| 修文县| 嘉峪关市| 扬中市| 库伦旗| 晋江市| 吉安县|