Why using IN(…) when selecting on indexed fields, will kill the performance of SELECT query?

后端 未结 4 720
青春惊慌失措
青春惊慌失措 2020-12-30 14:46

Avoid using IN(...) when selecting on indexed fields, It will kill the performance of SELECT query.

I found this here: https://wikis.ora

4条回答
  •  伪装坚强ぢ
    2020-12-30 15:06

    I believe IN is treated the same as a group of ORs, so using ORs won't help.

    An alternative is to create a temporary table to hold the values of your IN-clause and then join with that temporary table in your SELECT.

    For example:

    CREATE TEMPORARY TABLE temp_table (v VARCHAR)
    
    INSERT INTO temp_table  VALUES ('foo')
    INSERT INTO temp_table  VALUES ('bar')
    
    SELECT * FROM temp_table tmp, orig_table orig
    WHERE temp_table.v = orig.value
    
    DROP TEMPORARY TABLE temp_table
    

提交回复
热议问题