select  q1.autoid,q1.callid
from (select q.autoid,q.callID,q.msisdn,q.startTime,q.endTime,q.callTime
,q.call_status,q.touch_status,q.sell_status,q.seatID,lh.file_path recordfilename,q.comment,q.flag
from
(select  c.autoid autoid,c.caseid callID,c.caller msisdn,convert(char,Dateadd(second,0,c.sdate_d),120) startTime,c.call_enddate endTime,c.call_totallen callTime,c.call_status call_status,c.touch_status touch_status,c.sell_status sell_status,c.ghid seatID,
      max(l.sdate+l.etime) rtime,c.info comment,c.flag flag
from crm_case_history c left join talentel_log.dbo.log_hd201006 l
on c.caller=l.called
where c.call_enddate='' and c.call_begdate='' and convert(char,Dateadd(second,+10,c.sdate_d),120)>='2010-06-12' and convert(char,Dateadd(second,+10,c.sdate_d),120)<'2010-06-12 22:00:00'
group by c.autoid,c.caseid,c.caller,c.sdate_d,c.call_enddate,c.call_totallen,c.call_status,c.touch_status,c.sell_status,c.ghid,
     c.info,c.flag
) q left join
talentel_log.dbo.log_hd201006 lh
on lh.sdate=subString(q.rtime,0,9) and lh.etime=subString(q.rtime,9,len(q.rtime)) and lh.called=q.msisdn
)q1,
crm_callback_task_015 t,itsc_info i,
(select c.projectid projectid,c.plan_name,max(i.productid) productid
from crm_callback_plan c,itsc_product i
where c.autoid=i.plan_id
group by c.projectid,c.plan_name) p
where t.taskid=i.taskid and q1.callid=t.caseid and i.projectid=p.projectid数据量很少,最大的表也就4万多条数据,但是查询起来特别慢,很久不出结果。
如果红色部分改为select  * 查询就会快很多。谢谢

解决方案 »

  1.   

    select q1.autoid,q1.callid
    from (select q.autoid,q.callID,q.msisdn,q.startTime,q.endTime,q.callTime
    ,q.call_status,q.touch_status,q.sell_status,q.seatID,lh.file_path recordfilename,q.comment,q.flag
    from
    (select c.autoid autoid,c.caseid callID,c.caller msisdn,convert(char,Dateadd(second,0,c.sdate_d),120) startTime,c.call_enddate endTime,c.call_totallen callTime,c.call_status call_status,c.touch_status touch_status,c.sell_status sell_status,c.ghid seatID,
      max(l.sdate+l.etime) rtime,c.info comment,c.flag flag
    from crm_case_history c left join talentel_log.dbo.log_hd201006 l
    on c.caller=l.called
    where c.call_enddate='' and c.call_begdate='' and convert(char,Dateadd(second,+10,c.sdate_d),120)>='2010-06-12' and convert(char,Dateadd(second,+10,c.sdate_d),120)<'2010-06-12 22:00:00'
    group by c.autoid,c.caseid,c.caller,c.sdate_d,c.call_enddate,c.call_totallen,c.call_status,c.touch_status,c.sell_status,c.ghid,
      c.info,c.flag
    ) q left join
    talentel_log.dbo.log_hd201006 lh
    on lh.sdate=subString(q.rtime,0,9) and lh.etime=subString(q.rtime,9,len(q.rtime)) and lh.called=q.msisdn
    )q1,----返回280左右数据
    crm_callback_task_015 t,--4万左右
    itsc_info i,--不到10条
    (select c.projectid projectid,c.plan_name,max(i.productid) productid
    from crm_callback_plan c,itsc_product i
    where c.autoid=i.plan_id
    group by c.projectid,c.plan_name) p ---10条左右
    where t.taskid=i.taskid and q1.callid=t.caseid and i.projectid=p.projectid
      

  2.   

    【如果红色部分改为select * 查询就会快很多。】
    好像与一般的劝告正好相反啊,呵呵那就把红色部分改为select * ,然后在外面再套一层:select autoid,callid from (....) a
      

  3.   

    感觉sql语句太冗余。
    建议限制条件先进行筛选在做连接。
    尽量不要用‘’东西。很影响速度。