I'm using MySQL
I have three tables:
accounts {
account_id,
username
}
account_ips {
idaccount_ips,
account_id,
ip
}
account_bans {
ban_id
account_id,
expires
}
Need to get grouped count of accounts per ip that are not in bans table. (See query below)
I've tried the following, but it is way too slow (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;
Explain output the following:
1, 'PRIMARY', 'a', 'ALL', '', '', '', '', 304745, 'Using where; Using temporary; Using filesort'
2, 'DEPENDENT SUBQUERY', 'b', 'ALL', '', '', '', '', 1851, 'Using where'