我有一个这样的查询
SELECT DISTINCT p.id
FROM person p
INNER JOIN person_func pf1
ON p.id = pf1.person_id
INNER JOIN Func f1
ON f1.id = pf1.func_id
LEFT JOIN person_location pf2
ON p.id = pf2.person_id
LEFT JOIN Location f2
ON f2.id = pf2.location_id AND f2.val='1'
LEFT JOIN person_location pf3
ON p.id = pf3.person_id
LEFT JOIN Location f3
ON f3.id = pf3.location_id and f3.val='3'
WHERE f2.val IS NOT NULL OR f3.val IS NOT NULL;
通常有 9-10 个这样的连接。它运行得非常非常慢。我在 person_func(person_id)、person_location(person_id) 上添加了索引,但没有帮助。我可以做些什么来优化它?
一个例子 - SQLFiddle 例子