我正在使用 MySQL
我有三张桌子:
accounts {
account_id,
username
}
account_ips {
idaccount_ips,
account_id,
ip
}
account_bans {
ban_id
account_id,
expires
}
需要获取不在禁令表中的每个 ip 的帐户分组计数。(见下方查询)
我尝试了以下方法,但速度太慢(44s):
SELECT DISTINCT a.account_id, count(DISTINCT a.account_id)
FROM account_ips AS a
WHERE NOT EXISTS(
SELECT 1
FROM account_bans AS b
WHERE b.expires > 1340341272 AND b.account_id = a.account_id)
GROUP BY a.ip
HAVING count(DISTINCT a.account_id) > 3
ORDER BY count(DISTINCT a.account_id) DESC;
解释输出以下内容:
1, 'PRIMARY', 'a', 'ALL', '', '', '', '', 304745, 'Using where; Using temporary; Using filesort'
2, 'DEPENDENT SUBQUERY', 'b', 'ALL', '', '', '', '', 1851, 'Using where'