oracle10g - how to make run sql query fast the following query keep on running suggest me how to optimize it -
/* formatted on 2015/06/24 15:51 (formatter plus v4.8.5) */
select (select name dept deptid = department) name, sum (received), sum (notresponded) (select trim (regexp_substr (b.dept, '[^,]+', 1, level) ) department, count (*) received, count (*) - count (case when round ( ( to_date (to_char (clssndtm, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) - to_date (to_char (made, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) ) * 24 ) <= 1 round ( ( to_date (to_char (clssndtm, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) - to_date (to_char (made, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) ) * 24 ) else null end ) notresponded reportng a, routing b a.ticket = b.ticket , (1 = 1) , a.rptflag = 1 , a.clssndtm not null , companyid = 2682 connect instr (dept, ',', 1, level - 1) > 0 , trunc (made) between trunc (to_date ('06/01/2015', 'mm/dd/yyyy' ) ) , trunc (to_date ('06/23/2015', 'mm/dd/yyyy' ) ) group trim (regexp_substr (dept, '[^,]+', 1, level))) group department
firstly, want query readable, otherwise it's impossible see what's going on:
select (select name dept deptid = department) name, sum (received), sum (notresponded) (select trim (regexp_substr (b.dept, '[^,]+', 1, level) ) department, count () received, count () - count (case when round ( ( to_date (to_char (clssndtm, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) - to_date (to_char (made, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) ) * 24 ) <= 1 round ( ( to_date (to_char (clssndtm, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) - to_date (to_char (made, 'yyyy-mm-dd hh24:mi' ), 'yyyy-mm-dd hh24:mi' ) ) * 24 ) else null end ) notresponded reportng a, routing b a.ticket = b.ticket , (1 = 1) , a.rptflag = 1 , a.clssndtm not null , companyid = 2682 connect instr (dept, ',', 1, level - 1) > 0 , trunc (made) between trunc (to_date ('06/01/2015', 'mm/dd/yyyy' ) ) , trunc (to_date ('06/23/2015', 'mm/dd/yyyy' ) ) group trim (regexp_substr (dept, '[^,]+', 1, level)) ) group department
now, there's oracle specific stuff in there don't understand can see there's 2 different major steps here. there's first inner query, may or may not running efficiently. run on own see how goes. i'm going assume it's fine (it may not be).
the outer bit going suboptimal have correlated query in select clause. needs split out join:
select dept.name, sum (received), sum (notresponded) complex_inner_query inner join dept on dept.deptid = complex_inner_query.department group department
that may solve problems, if not need start analyzing inner query find inefficiency.
Comments
Post a Comment