SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Speed up Query by replacing subselects


Speed up Query by replacing subselects

Author
Message
rlsublime
rlsublime
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 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


weitzera
weitzera
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1360 Visits: 629
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.'

MMartin1
MMartin1
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7125 Visits: 2033
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.

----------------------------------------------------
How to post forum questions to get the best help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search