经过多次传递后,我想知道是否可以进一步优化此查询?任何见解都非常感谢。除了必须使用一堆不理想的子查询之外,我必须使用其中一个两次因为'tags'引用不能在where子句中使用...(一直无法弄清楚如何将其重写为联接以解决该问题
SELECT `t1`.*,
(SELECT GROUP_CONCAT(tag_id) FROM media_tag WHERE media_id=t1.id GROUP BY media_id ) as tags,
(SELECT GROUP_CONCAT(award_id) FROM media_award WHERE media_id=t1.id GROUP BY media_id ) as awards,
(SELECT GROUP_CONCAT(client_id) FROM media_client WHERE media_id=t1.id GROUP BY media_id ) as clients,
(SELECT GROUP_CONCAT(collaborator_id) FROM media_collaborator t2 LEFT JOIN collaborator t3 ON t2.collaborator_id=t3.id WHERE media_id=t1.id AND t3.collaborator_type_id='1' GROUP BY media_id ) as agencies,
(SELECT GROUP_CONCAT(collaborator_id) FROM media_collaborator t2 LEFT JOIN collaborator t3 ON t2.collaborator_id=t3.id WHERE media_id=t1.id AND t3.collaborator_type_id<>'1' GROUP BY media_id ) as collaborators,
`t2`.`idx`, `t2`.`type`
FROM (`media` t1)
LEFT JOIN `sort` t2 ON `t1`.`id`=`t2`.`id` AND t2.type="media"
WHERE
FIND_IN_SET('1',(SELECT GROUP_CONCAT(tag_id) FROM media_tag WHERE media_id=t1.id GROUP BY media_id ))
AND IF(t2.tag_id IS NOT NULL,t2.tag_id='1',1)
GROUP BY `t1`.`id`
ORDER BY `t1`.`date_mod` DESC, `title` ASC, `t2`.`idx` ASC, `date_mod` DESC, `title` ASC
答案 0 :(得分:2)
你应该尝试最小化每行的select语句,你可以通过一些标识符连接那些行和组,如果你可以发布你的表结构并告诉我们你想从他们那里得到什么信息会更好...... / p>