有一条语句需要改写
select count(distinct(id))  ,  case when.....  ---(条件相当多)
from table
...group by a.b在系统发现这个语句在高峰期间很很耗资源,但我开session是相当快。
思考半天只能 count(distinct(id)  将它改写 (近3个月的experience
知道有distinct就不是好事情,他会很慢)现在有意思将它替换,问有什么方便的方法?
(我的初步想法
select  
group by  having count(×) 》2
 》
---可以实现distinct
但是我不知道count(distinct)如何用 group by实现
)??

解决方案 »

  1.   

    把distinct 后的字段改为group by 后的不就好了吗,然后在前面用count(group by后的字段)
      

  2.   

    原语句骨干为
    select  count(distinct(ul.Id)) number,      CASE        THEN 4         ELSE 5  END as Interval
     from        SessionLog ul,User u  where   u.id = ul.id group by Interval;请注意group by的字段和count(distinct(ul.Id)) 中的字段是不同的哦啦