优化“ COUNT(DISTINCT col)WHERE othercol =?”为了速度

时间:2018-11-04 22:02:01

标签: mysql sql performance query-optimization

我有一个独特的计数,大约需要半分钟或更长时间才能得到结果。

select COUNT(DISTINCT ip) as unique_visits
from idevaff_iptracking
where acct_id = ? and stamp = ?

计算IP的更快方法是什么? ip列和带有ip列的acct_id上都有索引。

1 个答案:

答案 0 :(得分:2)

对于此查询:

select COUNT(DISTINCT ip) as unique_visits
from idevaff_iptracking
where acct_id = ? and stamp = ?

您要在idevaff_iptracking(acct_d, stamp, ip)上建立索引。