Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Speed up Query by replacing subselects Expand / Collapse
Author
Message
Posted Thursday, March 13, 2014 4:40 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 20, 2014 8:25 PM
Points: 7, Visits: 11

I am trying to speed up the below code, which includes numerous subselect statements. Any thoughts on how I could alter the statements for the code to run faster. The queries will be running in Oracle environment.

Thanks

 Select prod_month, lockbox, company, workgrp, checks, Unprocs, NoChk, frontim, checktotal, keystrokes,
(checks+unprocs+NoChk) as TotalCKNCUP,
case when checktotal > 0 and checks > 0
then round(checktotal / checks,2)
else 0 end as AvgDollarCheck,
ocr_stub,
nvl((Select sum(error_count) from errors_kpi_isd err
where err.lockboxnumber = oq.lockbox
and err.product = oq.donorsite
and substr(err.errortype,1,3) not in ('DKY','FYI','IMG')
and err.inquirydate between oq.prod_month and add_months(oq.prod_month,1)-1),0) as NonDKYerrorsforMonth,
nvl((Select sum(error_count) from errors_kpi_isd err
where err.lockboxnumber = oq.lockbox
and err.product = oq.donorsite
and substr(err.errortype,1,3) in ('DKY')
and err.inquirydate between oq.prod_month and add_months(oq.prod_month,1)-1),0) as DKYerrorsforMonth,
(Select checks from r1_monthly_volumes iq
Where iq.prod_month = add_months(oq.prod_month,-1)
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid) as ckslastmo,
case when (Select checks from r1_monthly_volumes iq
Where iq.prod_month = add_months(oq.prod_month,-1)
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid) > 0
and oq.checks > 0
then round(((Select checks from r1_monthly_volumes iq
Where iq.prod_month = add_months(oq.prod_month,-1)
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid)-oq.checks)/(Select checks from r1_monthly_volumes iq
Where iq.prod_month = add_months(oq.prod_month,-1)
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid),4)*-1
else 0 end as ckchangefromlastmo,
(Select sum(checks) from r1_monthly_volumes iq
where iq.prod_month between add_months(prod_month,-to_number(to_char(Prod_month,'MM'),'99')+1)
and oq.prod_month
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid) as YTD_Checks,
case when (Select sum(checks) from r1_monthly_volumes iq
where iq.prod_month between add_months(prod_month,-to_number(to_char(Prod_month,'MM'),'99')+1)
and oq.prod_month
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid) > 0
then round((Select sum(checks) from r1_monthly_volumes iq
where iq.prod_month between add_months(prod_month,-to_number(to_char(Prod_month,'MM'),'99')+1)
and oq.prod_month
and iq.lockbox = oq.lockbox
and iq.siteid = oq.siteid) / to_number(to_char(prod_month,'MM'),'99'),0)
else 0 end as YTDMonthlyAvgChecks,
nvl((Select sum(error_count) from errors_kpi_isd err
where err.lockboxnumber = oq.lockbox
and err.product = oq.donorsite
and substr(err.errortype,1,3) not in ('DKY','FYI','IMG')
and err.inquirydate between add_months(prod_month,-to_number(to_char(Prod_month,'MM'),'99')+1)
and add_months(oq.prod_month,1)-1),0) as YTDNonDKYErrorCount,
nvl((Select sum(error_count) from errors_kpi_isd err
where err.lockboxnumber = oq.lockbox
and err.product = oq.donorsite
and substr(err.errortype,1,3) in ('DKY')
and err.inquirydate between add_months(prod_month,-to_number(to_char(Prod_month,'MM'),'99')+1)
and add_months(oq.prod_month,1)-1),0) as YTDDKYErrorCount,
case when (Select sum(checks) from r1_monthly_volumes iq
where iq.prod_month between add_months(prod_month,-to_number(to_char(Prod_month,'MM'),'99')+1)
and oq.prod_month

Post #1550960
Posted Monday, May 26, 2014 1:50 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 2:51 PM
Points: 880, Visits: 607
In sql server, it's generally better to refactor your subselects to a join. I don't know how much that applies to oracle. Will you be running this across a connection to oracle from sql server? (e.g. openquery)

An example of refactoring your query:
Select prod_month, lockbox, company, workgrp, checks, Unprocs, NoChk, frontim, checktotal, keystrokes,
(checks+unprocs+NoChk) as TotalCKNCUP,
case when checktotal > 0 and checks > 0
then round(checktotal / checks,2)
else 0 end as AvgDollarCheck,
ocr_stub,
nvl(errorCounts.errorCount,0) as NonDKYerrorsforMonth
--...
from oq
left join (Select err.lockboxnumber, err.product, month(inquirydate) inquiryMonth, year(inquiryDate) inquiryYear, sum(error_count) as errorCount
from errors_kpi_isd err
where substr(err.errortype,1,3) not in ('DKY','FYI','IMG')
group by err.lockboxnumber, err.product, month(inquirydate), year(inquiryDate)) errorCounts
on errorCounts.lockboxnumber = oq.lockbox
and errorCounts.product = oq.donorsite
and cast(cast(errorCounts.inquiryYear as char(4)) + '-' + cast(errorCounts.inquiryMonth as varchar(2)) + '-1' as datetime) = oq.inquiryMonth

Note that I used sql server syntax for the new code. Also note that in order to reap the benefits of this, you should try to join to each table as few times as possible, so you'll want to do some work to replace the errortype filter and use multiple groups, or to use windowed aggregates if those are available in your oracle environment. I'm not sure you'll get much help on oracle-specific syntax or functionality here.

I hope that helps





Dan Guzman - Not the MVP (7/22/2010)
All questions have to be prefaced by Server version and 'according to MS Docs' or 'my own personal opinion based on how much detail I felt like digging into at the time.'
Post #1574593
Posted Tuesday, July 22, 2014 2:07 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 6:00 PM
Points: 353, Visits: 868
Its possible, dont know if it will happen as we dont have DDL definitions and data here, for the two queries two produce the same execution plan on SQL Server. But the second version is defeintely more readable and easier to maintain. Just like Sql Server has a index advisor (not great) and a execution plan... see if Oracle has similar tools to guide your query development.
Post #1595274
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse