此查询的效率如何。它在子查询中从同一个表中选择两次。
SELECT DISTINCT country, rowCount FROM
(
SELECT *, (
SELECT COUNT(id) FROM tbl WHERE hide != 1 AND country = y.country
) AS rowCount FROM tbl y
) AS x
HAVING rowCount >= 1
ORDER BY x.country ASC
我已经将表索引到需要的位置,并且查询在“0.000秒”内执行(HeidiSQL说)所以它一定很好,但有什么可以改善查询的结构吗?
答案 0 :(得分:3)
似乎可以将其重写为单个查询:
SELECT country, COUNT(id) rowCount
FROM tbl
WHERE hide <> 1
GROUP BY country
HAVING rowCount >= 1