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

后端 未结 4 713
青春惊慌失措
青春惊慌失措 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:17

    Prior to MySQL 5.0 it seems that mySQL would only use a single index for a table. So, if you had a SELECT * FROM tbl WHERE (a = 6 OR b = 33) it could chooose to use either the a index or the b index, but not both. Note that it says fields, plural. I suspect the advice comes from that time and the work-around was to union the OR results, like so:

    SELECT * FROM tbl WHERE (a = 6)
    UNION
    SELECT * FROM tbl WHERE (b = 33)
    

提交回复
热议问题