首页 > 解决方案 > 优化“COUNT(DISTINCT col) WHERE othercol = ?” 为了速度

问题描述

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

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

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

标签: mysqlsqlperformancequery-optimization

解决方案


对于此查询:

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

你想要一个关于idevaff_iptracking(acct_d, stamp, ip).


推荐阅读