Performance of MYSQL “IN”

前端 未结 2 843
说谎
说谎 2020-12-05 05:00

I\'m running a MYSQL query in two steps. First, I get a list of ids with one query, and then I retrieve the data for those ids using a second query along the lines of

相关标签:
2条回答
  • 2020-12-05 05:23

    Starting from a certain number of records, the IN predicate over a SELECT becomes faster than that over a list of constants.

    See this article in my blog for performance comparison:

    • Passing parameters in MySQL: IN list vs. temporary table

    If the column used in the query in the IN clause is indexed, like this:

    SELECT  *
    FROM    table1
    WHERE   unindexed_column IN
            (
            SELECT  indexed_column
            FROM    table2
            )
    

    , then this query is just optimized to an EXISTS (which uses but a one entry for each record from table1)

    Unfortunately, MySQL is not capable of doing HASH SEMI JOIN or MERGE SEMI JOIN which are yet more efficient (especially if both columns are indexed).

    0 讨论(0)
  • 2020-12-05 05:45

    Why do you extract the ids first? You should probably just join the tables. If you use the ids for something else, you can insert them in a temp table before and use this table for the join.

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