我有这个查询返回以下内容:
- 地区名称
- 区域编号
- 该区域的排名(位置),基于总票数
- Nb 来自该地区的不同用户
- Nb 来自该地区的不同照片
查询大约需要 7.5 秒才能完成......我想要一些建议来优化我的查询。
select
WrappedQuery.*,
regions.name as region_name,
regions.id as region_id,
count(distinct users.id) as nb_users,
count(distinct photos.id) as nb_photos
from (
select
@rownum := @rownum +1 as rank,
prequery.region_id,
prequery.VoteCount
from
( select @rownum := 0 ) sqlvars,
( select region_id, count(id) VoteCount
from votes
where theme_id = '{$currentTheme}'
group by region_id
order by count(id) desc ) prequery
) WrappedQuery, regions, users, photos
WHERE regions.id = WrappedQuery.region_id
AND users.region_id = WrappedQuery.region_id
AND photos.region_id = WrappedQuery.region_id
GROUP BY WrappedQuery.region_id
ORDER BY WrappedQuery.rank ASC
LIMIT 0, 1
提前非常感谢。