求个能把一对多变成 一对一,或者更好的方案
有个联系表记录, table A
现在要统计,某个月份内,联系各客户等级的数量.
关联 table B 看这个客户的等级
A表一个月内大概 10w条数据 , 然后客户信息表,也是10w条. 每次查询都要20秒左右,才可以
SELECT b.label,count(distinct b.namel) FROM table AS a
RIGHT JOIN table AS b ON a.`relation_id` = b.id
where a.`create_time` >= '2017-04-01 00:00:00'
and a.`create_time` < '2017-05-01 00:00:00'
where a.`type` = 'client'
GROUP BY b.label;
现在的思路就是 看能不能,在 where 的时候就把 重复的 relation_id 给过滤掉?
不过我把 type 和 create_time 和 relation_id 建立成一个索引了
也不知道是不是,索引创建有误,望指点一下.