针对MySQL 的distinct
优化
优化count(distinct 字段)
问题
使用distinct查询去重后的count
测试数据
●表名:A
●总行数为:718w行
●不重复行数:443w行
原始方案:
select count(DISTINCT pid) from A
运行耗时:204.264秒
优化方案1:
select count(*) from (select DISTINCT pid from A) as temp
运行耗时:130.528秒
优化方案2:
select count(*) from (select pid from A GROUP BY pid) as temp
运行耗时:136.173秒