我的测试给出了相同的运行速度。我想弄清楚最好使用加入或子查询。我理解这很大程度上取决于数据,索引和 DB 本身,但我想区分它,以便 t1 中的记录小于 t2 然后子查询应该有效,但如果相反则加入。
EXPLAIN ANALYSE SELECT *
FROM safety_policies
WHERE safety_policies.area_id IN (SELECT areas.id
FROM areas
INNER JOIN departments ON areas.department_id = departments.id);
EXPLAIN ANALYSE SELECT safety_policies.*
FROM safety_policies
INNER JOIN areas ON safety_policies.area_id = areas.id
INNER JOIN departments ON areas.department_id = departments.id;