Mysql 如何进行更快的最大n个组查询?
我正在使用此查询:Mysql 如何进行更快的最大n个组查询?,mysql,greatest-n-per-group,Mysql,Greatest N Per Group,我正在使用此查询: SELECT district, id FROM adverts ls GROUP BY district, id HAVING ( SELECT count( * ) FROM adverts WHERE district = ls.district AND id > ls.id ) <5 ORDER BY district, id DESC ; LIMIT 0 , 30 有没有办法加快速度?试试这个: SELECT district, id, CO
SELECT district, id
FROM adverts ls
GROUP BY district, id
HAVING (
SELECT count( * )
FROM adverts
WHERE district = ls.district
AND id > ls.id
) <5
ORDER BY district, id DESC ;
LIMIT 0 , 30
有没有办法加快速度?试试这个:
SELECT district, id, COUNT(b.district)
FROM adverts a INNER JOIN adverts b
ON a.district = b.district
WHERE b.id > a.id
GROUP BY district, id
HAVING COUNT(b.district) < 5
ORDER BY district, id DESC
选择地区、id、计数(b区)
从广告a内部连接广告b
在a区=b区
其中b.id>a.id
按地区分组,id
计数(b区)<5
按地区订购,id描述
根据定义,连接
比子查询
更快 试试这个:
SELECT district, id, COUNT(b.district)
FROM adverts a INNER JOIN adverts b
ON a.district = b.district
WHERE b.id > a.id
GROUP BY district, id
HAVING COUNT(b.district) < 5
ORDER BY district, id DESC
选择地区、id、计数(b区)
从广告a内部连接广告b
在a区=b区
其中b.id>a.id
按地区分组,id
计数(b区)<5
按地区订购,id描述
根据定义,连接
比子查询
更快