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

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)

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.'

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.