Select query with three where conditions is slow, but the same query with any combination of two of the three where conditions is fast

前端 未结 3 1317
暖寄归人
暖寄归人 2021-01-03 08:33

I have the following query:

SELECT table_1.id

FROM
table_1
LEFT JOIN table_2 ON (table_1.id = table_2.id)

WHERE
table_1.col_condition_1 = 0
AND table_1.col         


        
3条回答
  •  北海茫月
    2021-01-03 09:32

    • OR is a performance killer.
    • Sometimes using UNION instead of OR can speed up the query.
    • Perhaps in one case the 5000 were "near the beginning" of the combined tables, but not in the other case.
    • Using LIMIT without ORDER BY is dubious.
    • Since a PK is a Unique key, it is redundant to also declare id_UNIQUE.
    • INDEX(a) is unnecessary when you also have INDEX(a,b).
    • If there are only 4 values, IN (1, 2) might be faster than NOT IN (3, 4).
    • It is unusual to have two tables sharing the same PK. Why do you have a 1:1 relationship?
    • We might have further insight if we could see the real column names.

提交回复
热议问题