我从不同的表中获取记录并避免重复行我使用GROUP BY
子句,DISTINCT
比GROUP BY
我尝试使用(作为替换)的大量JOIN
查询要快,但它也获取重复记录。知道如何实现优化、最快和高效的查询。DISTINCT
GROUP BY
查询样本:
这里索引是projectId
在项目表中的字段上设置的。
SELECT DISTINCT p.*, p.projectId as proId, plat.*, con.*, dom.*, sta.*, tech.*, role.*, pimg.*, c.*, dur.*
FROM projects p
LEFT JOIN projectplatforms pplat ON p.projectId = pplat.projectId JOIN platforms plat ON plat.platformId = pplat.platformId
LEFT JOIN projectcountries pcon ON p.projectId = pcon.projectId JOIN countries con ON con.countryId = pcon.countryId
LEFT JOIN projectdomains pdom ON p.projectId = pdom.projectId JOIN domains dom ON dom.domainId = pdom.domainId
LEFT JOIN statuses sta ON p.statusId = sta.statusId
LEFT JOIN projecttechs ptech ON p.projectId = ptech.projectId JOIN technologies tech ON tech.techId = ptech.techId
LEFT JOIN projectroles prole ON p.projectId = prole.projectId JOIN roles role ON role.roleId = prole.roleId
LEFT JOIN projectimages pimg ON p.projectId = pimg.projectId
LEFT JOIN clients c ON p.clientId = c.clientId
LEFT JOIN durations dur ON p.durationId = dur.durationId
WHERE p.featured = 1
ORDER BY p.year DESC