假设此查询:
SELECT x.*
FROM (
SELECT COALESCE(p2.id,p1.id) id,
COALESCE(p2.subject,p2.subject) subject,
body
FROM posts p1
LEFT JOIN posts p2 ON p1.related = p2.id
WHERE MATCH(subject,body) AGAINST(:entry)
) x INNER JOIN pivot p ON x.id = p.post_id
INNER JOIN tags t ON t.id = p.tag_id AND t.name = "tag_name"
如您所见,x
是一个虚拟表,x.id
不是真正的列。此外,x.id
已加入pivot
上的pivot.post_id
。它会带来索引的好处吗?如果没有,解决方案是什么?