SQL fixed-value IN() vs. INNER JOIN performance

前端 未结 1 822
眼角桃花
眼角桃花 2020-12-11 17:58

In answer to this SQL question, I\'ve encountered a statement that fixed-value IN() operator is much slower than INNER JOIN with the same content,

相关标签:
1条回答
  • 2020-12-11 18:46

    This relates to the length of the IN clause - and what is sometimes called a BUG in MySQL.

    MySQL seems to have a low threshold for IN clauses, when it will swap to a TABLE/INDEX SCAN instead of collecting multiple partitions (one per IN item) and merging them.

    With an INNER JOIN, it is almost always forced to use a direct row-by-row in JOIN collection, which is why it is sometimes faster

    Refer to these MySQL manual pages

    • In Subquery considerations
    • In (constant list) performance

    I could be wrong since it seems to imply that IN (constant value list) should always use a binary search on each item...

    0 讨论(0)
提交回复
热议问题