日期:2014-05-17  浏览次数:20689 次

如何优化这句sql
注:回访表   eb_recall_history   与   eb_recall_result   关联     。eb_recall_history数据量很大,下面的这句SQL能达到要求,但查询速度很慢,寻求优化方案。

select  
        (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =184)   呼入限制,
        (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =185)   不知情,
        (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =170)   电话关机,
          (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =171)   用户拒访,
          (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =172)   欠费停机,
          (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =173)   传真电话,
          (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =174)   无人接听,
          (select   rr_qi_id   from   eb_recall_result   where   rr_qq_id   =   173   and   rr_is_select   =1   and   rr_rec_id=rec_id   and   rr_qi_id   =175)   无法接通
    from   eb_recall_history  


------解决方案--------------------
select decode(b.rr_qi_id,184, '呼入限制 ',185, '不知情 ',170, '电话关机 ',171, '用户拒访 ',172, '欠费停机 ',
173, '传真电话 ',174, '无人接听 ',175, '无法接通 ', ' ')
from eb_recall_history a,eb_recall_result b
where b.rr_qq_id = 173 and b.rr_is_select =1 and b.rr_rec_id=a.rec_id
------解决方案--------------------
select
max(decode(t2.rr_qi_id,184,rr_qi_id)) 呼入限制,
max(decode(t2.rr_qi_id,185,rr_qi_id)) 不知情,
max(decode(t2.rr_qi_id,170,rr_qi_id)) 电话关机,
max(decode(t2.rr_qi_id,171,rr_qi_id)) 用户拒访,
max(decode(t2.rr_qi_id,172,rr_qi_id)) 欠费停机,
max(decode(t2.rr_qi_id,173,rr_qi_id)) 传真电话,
max(decode(t2.rr_qi_id,174,rr_qi_id)) 无人接听,
max(decode(t2.rr_qi_id,175,rr_qi_id)) 无法接通
from
eb_recall_history t1,eb_recall_result t2
where t2.rr_rec_id=t1.rec_id and
t2.rr_is_select=1 and
t2.rr_qq_id=173
group by t1.rec_id