Home > Database > Mysql Tutorial > Why is SQL's `IN` Condition Sometimes Slower Than the `=` Operator?

Why is SQL's `IN` Condition Sometimes Slower Than the `=` Operator?

Mary-Kate Olsen
Release: 2025-01-16 17:13:08
Original
152 people have browsed it

Why is SQL's `IN` Condition Sometimes Slower Than the `=` Operator?

SQL's IN Condition: Performance Issues Compared to =

In specific situations, SQL's IN condition can significantly underperform compared to the = operator. This was a notable issue in older MySQL versions, though later versions (MySQL 5.6 and beyond) have addressed this.

The performance difference stems from optimization challenges. A subquery within an IN clause might be incorrectly identified as dependent, rather than independent. This leads to the subquery's repeated execution for each row in the main query, dramatically impacting performance.

Let's look at an example:

<code class="language-sql">SELECT * FROM question_law_version
WHERE question_law_id IN (
    SELECT MAX(foo_id)
    FROM bar
);</code>
Copy after login

An EXPLAIN plan for this query might reveal:

<code>1  'PRIMARY'             'question_law_version'  'ALL'  ''  ''  ''  ''  10148  'Using where'
2  'DEPENDENT SUBQUERY'  'question_law_version'  'ALL'  ''  ''  ''  ''  10148  'Using where'
3  'DEPENDENT SUBQUERY'  'question_law'          'ALL'  ''  ''  ''  ''  10040  'Using where'</code>
Copy after login

Notice the "DEPENDENT SUBQUERY" designation. The inner query is repeatedly executed. However, rewriting the query using = (assuming the subquery returns a single value):

<code class="language-sql">SELECT * FROM question_law_version
WHERE question_law_id = (
    SELECT MAX(foo_id)
    FROM bar
);</code>
Copy after login

...often results in a more efficient plan:

<code>1  'PRIMARY'   'question_law_version'  'ALL'  ''  ''  ''  ''  10148  'Using where'
2  'SUBQUERY'  'question_law_version'  'ALL'  ''  ''  ''  ''  10148  'Using where'
3  'SUBQUERY'  'question_law'          'ALL'  ''  ''  ''  ''  10040  'Using where'</code>
Copy after login

The key difference is the "SUBQUERY" classification, indicating a single execution. This performance discrepancy arises from MySQL's (in older versions) misinterpretation of the IN clause's subquery dependency. The database's inefficient handling of this leads to the performance slowdown.

The above is the detailed content of Why is SQL's `IN` Condition Sometimes Slower Than the `=` Operator?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template