A MySQL bug about exists, MySQLexistsbug

Source: Internet
Author: User
Tags floor function

A MySQL bug about exists, MySQLexistsbug

Today, I encountered a strange question about exists.

The first statement is as follows:

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

Result: 89584

The second statement is as follows:

SELECTcount(1)FROMAPPLY tWHEREEXISTS (SELECTmax(r.FINISH_TIME)FROMRECORD rWHEREt.APPLY_ID = r.APPLY_ID);

Result: 432382

It is indeed quite strange that for the exist clause, it determines whether the subquery value exists, that is, the column name is no different from the maximum value for the column name.

It is also mentioned in the MySQL official documentation.

Traditionally, an EXISTS subquery starts with SELECT *, but it cocould 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 automatically ignores the SELECT list.

Later I tested it in my own environment. It was indeed a MySQL bug.

Test environment: 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 |

Obviously, the column with id equal to 2 does not exist, but the second statement is processed as TRUE.

The execution plan of the next two statements and the SQL statement after rewriting are also confirmed.

First Statement

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 |+---------+------+-------------------------------------------------------------------+ 

Second statement

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) 

The execution plan and the modified SQL statement are indeed different. It seems that it is indeed a bug in MySQL.

So I raised a bug to the official website.

Http://bugs.mysql.com/bug.php? Id = 82562

Summary

We recommend that you use * directly in the subquery when writing the exists statement, instead of performing any function operations on the column to avoid encountering official bugs,

In fact, the floor function is okay for abs.

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)

The above is a MySQL bug about exists introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.