我试图优化这个有一段时间了。这是一个巨大查询的一小部分,但这就是查询最终变慢的原因。我认为您可以从中获得简单的表结构。有人知道如何做到这一点优雅和苗条吗?
SELECT gref.gid AS gid, COUNT(o.id) AS cnt
FROM objects AS o
RIGHT JOIN group_xref AS gx ON o.id = gx.oid
LEFT JOIN group_xref AS gref ON o.id = gref.oid
LEFT JOIN groups AS groups ON gref.gid = groups.id
WHERE groups.att = 1
GROUP BY gref.gid
好的,这里是解释:http: //htmlpaste.com/ff849935d884a36076f74bd934b0212bac7658e9