sql server - Inefficient sql query with joined tables -
i have table 1700 rows of data. i'm querying using query below pulls complimentary data related tables too. it's running (around 10 seconds).
i'd appreciate help/tips improving efficiency of query.
many in advance.
select [jobid] , (select orgname pm_clients c c.orgid in (select orgid pm_jobs j j.jobid=t.jobid , j.jobstatus>=13)) orgname , (select sector pm_clients c c.orgid in (select orgid pm_jobs j j.jobid=t.jobid , j.jobstatus>=13)) sector , (select region= case country when 1 region else (select countryname at_a_countrycodes x x.id= l.country) end pm_clientdetails l l.userid = (select userid pm_jobs j j.jobid=t.jobid)) region , (select postcode pm_clientdetails l l.userid = (select userid pm_jobs j j.jobid=t.jobid)) postcode , (select firstname users u u.userid = (select pmid pm_jobs j j.jobid=t.jobid)) pm , [creationdate] , (select statusname pm_jobstatus j j.[statusid]=t.jobstatus) jobstatus , [completiondate] , [deadline], [jobtitle] , (select currencysymbol at_a_currency c c.currencyid = (select top(1) quotecurrency pm_quotes q q.taskid in (select taskid pm_tasks x x.jobid=t.jobid))) currency , (select sum(quotesubtotal) pm_quotes q q.taskid in (select taskid pm_tasks x x.jobid=t.jobid)) subtotal , (select sum(quotevat) pm_quotes q q.taskid in (select taskid pm_tasks x x.jobid=t.jobid)) vat , (select sum(quotetotal) pm_quotes q q.taskid in (select taskid pm_tasks x x.jobid=t.jobid)) total , (select [purchaseorder] pm_jobs j j.jobid=t.jobid) purchaseorder , (select [clientreference] pm_jobs j j.jobid=t.jobid) clientreference , (select case when [deadline]='1900-01-01 00:00:00' 1 when [completiondate]>dateadd(dd,1,[deadline]) 0 when [completiondate]<=dateadd(dd,1,[deadline])then 1 when [completiondate] null 0 end) completedontime [pm_jobs] t jobstatus>=13
edit @guidog quick response. here's amended query faster now.
select j.jobid, c.orgname, c.sector, (select region= case country when 1 region else (select countryname at_a_countrycodes x x.id = l.country) end) region, l.postcode, (select firstname users u u.userid = j.pmid) pm, j.creationdate, (select statusname pm_jobstatus x x.[statusid] = j.jobstatus) jobstatus, j.[completiondate], j.[deadline], j.[jobtitle], j.purchaseorder, j.clientreference, (select currencysymbol at_a_currency c c.currencyid = l.clientcurrency) currency, sum(q.quotesubtotal) subtotal, sum(q.quotevat) vat, sum(q.quotetotal) total, (select case when j.[deadline] = '1900-01-01 00:00:00' 1 when j.[completiondate] > dateadd(dd, 1, j.[deadline]) 0 when j.[completiondate] <= dateadd(dd, 1, j.[deadline])then 1 when j.[completiondate] null 0 end) completedontime, count(t.taskid) taskcount [pm_jobs] j inner join pm_clients c on j.orgid = c.orgid inner join pm_clientdetails l on j.userid = l.userid inner join pm_tasks t on j.jobid = t.jobid inner join pm_quotes q on q.taskid = t.taskid , t.jobid = j.jobid jobstatus >= 13 group j.jobid, c.orgname, c.sector, l.country, l.region, l.postcode, l.firstname, j.creationdate, j.jobstatus, j.completiondate, j.deadline, j.jobtitle, j.purchaseorder, j.clientreference, l.clientcurrency, j.pmid order completiondate desc
you should consider joining in stead of subquerying.
small example on way :
select t.jobid , c.orgname, c.sector [sql2012_921487_atlas].[dbo].[pm_jobs] t inner join pm_clients c on t.orgid = c.orgid jobstatus>=13
when subquerying u did u force sql server read table pm_clients several times, joining enables read pm_clients 1 time
Comments
Post a Comment