ORACLE

这几天头让我做一个有上亿条记录的表的查询优化:
这是优化前:
select  
	rs0.teamname 所属团队, 
	rs0.agentid 座席号,
	rs0.agentname 座席名称,
	nvl(rs8.jieting,0) 呼入数,
	nvl(rs7.huchu,0) 呼出数,
	nvl(rs1.zhengli,0) 处理次数,
	nvl(rs6.refuse,0) 拒绝,
	nvl(rs5.zhulixi,0) 离席数,
	nvl(rs3.jianting,0) 监听,
	nvl(rs4.bangzhu,0) 帮助,
	nvl(rs2.zxjth,0) 坐席间通话,
	nvl(rs9.guaji,0) 振铃数秒未接起用户挂机  
from 
	(select agentid ,agentname,ctiagentid,departname,teamname,dicvalue  from  s_view_userinfo where 1=1) rs0,
	
	(select count(*) zhengli,agtid 
	from sys2_log_agtst 
	where starttime >='20120301000000' and 
	starttime <='20120331235959' and agtstatus=13  and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs1,
	
	(select count(*) zxjth,agtid 
	from sys2_log_agtst 
	where (starttime >='20120301000000' and 
	starttime <='20120331235959' and agtstatus='10') and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs2,
	
	(select count(*) jianting,agtid 
	from sys2_log_agtst 
	where (starttime >='20120301000000' and 
	starttime <='20120331235959' and agtstatus='5') and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs3,
	
	(select count(*) bangzhu,agtid 
	from sys2_log_agtst 
	where (starttime >='20120301000000' and 
	starttime <='20120331235959' and agtstatus='6') and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs4,
	
	(select count(*) zhulixi ,agtid 
	from sys2_log_agtst 
	where (starttime >='20120301000000' and starttime <='20120331235959' and 
	agtstatus='4') and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs5,
	
	(select count(*) refuse,agtid 
	from sys2_log_agtst 
	where  (starttime >='20120301000000' and starttime <='20120331235959') and (agtstatus = '71' or agtstatus ='81') and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs6,
	
	(select count(agtid) huchu,agtid 
	from sys2_log_agtst 
	where (starttime >='20120301000000' and 
	starttime <='20120331235959') and agtstatus=18 and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1) 
	group by agtid) rs7,
	
	(select count(agtid) jieting,agtid 
	from sys2_log_agtst 
	where (starttime >='20120301000000' and 
	starttime <='20120331235959') and agtstatus=3 and timespan>=20 and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1) 
	group by agtid) rs8, 
	
	(select count(*) guaji,agtid 
	from sys2_log_agtst  
	where (starttime >='20120301000000' and 
	starttime <='20120331235959') and (agtstatus=73) and (timespan>=20) and 
	agtid in (select ctiagentid  from s_view_userinfo where 1=1 ) 
	group by agtid) rs9 
	
where 
	rs0.ctiagentid=rs1.agtid(+) and 
	rs0.ctiagentid=rs2.agtid(+) and 
	rs0.ctiagentid=rs3.agtid(+) and 
	rs0.ctiagentid=rs4.agtid(+) and 
	rs0.ctiagentid=rs5.agtid(+) and 
	rs0.ctiagentid=rs6.agtid(+) and 
	rs0.ctiagentid=rs7.agtid(+) and 
	rs0.ctiagentid=rs8.agtid(+) and 
	rs0.ctiagentid=rs9.agtid(+) 
order by rs0.departname,rs0.teamname,rs0.agentid






这是优化后:

select  
  rs0.teamname 所属团队, 
  rs0.agentid 座席号,
  rs0.agentname 座席名称,
  nvl(rs1.jieting,0) 呼入数,
  nvl(rs1.huchu,0) 呼出数,
  nvl(rs1.zhengli,0) 处理次数,
  nvl(rs1.refuse,0) 拒绝,
  nvl(rs1.zhulixi,0) 离席数,
  nvl(rs1.jianting,0) 监听,
  nvl(rs1.bangzhu,0) 帮助,
  nvl(rs1.zxjth,0) 坐席间通话,
  nvl(rs1.guaji,0) 振铃数秒未接起用户挂机  
from 
  (select agentid ,agentname,ctiagentid,departname,teamname,dicvalue  from  s_view_userinfo where 1=1) rs0,
  
  (select agtid ,
  sum(case when agtstatus = 13 then 1 else 0 end) zhengli,
  sum(case when agtstatus = 10 then 1 else 0 end) zxjth,
  sum(case when agtstatus = 5 then 1 else 0 end) jianting,
  sum(case when agtstatus = 6 then 1 else 0 end) bangzhu,
  sum(case when agtstatus = 4 then 1 else 0 end) zhulixi ,
  sum(case when (agtstatus = 71 or agtstatus =81) then 1 else 0 end) refuse,
  sum(case when agtstatus = 18 then 1 else 0 end) huchu,
  sum(case when agtstatus = 3 then 1 else 0 end) jieting,
  sum(case when (agtstatus=73) then 1 else 0 end) guaji
  
  from sys2_log_agtst t1
  where starttime >='20120301000000' and
  starttime <='20120331235959' and 
  timespan>=20 and 
   exists  (select 'x'  from s_view_userinfo t2 where t1.agtid=t2.ctiagentid ) 
  group by agtid) rs1

where 
  rs0.ctiagentid=rs1.agtid(+) 
order by rs0.departname,rs0.teamname,rs0.agentid

你可能感兴趣的:(oracle)