Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to optimize T-SQL Query?
    primarykey
    data
    text
    <p>I am trying to optimize the following query to increase the performance particularly the joins. I would greatly appreciate any suggestions and help. Thanks a lot. I am thinking to use CTE instead of subqueries</p> <p>Query:</p> <pre><code>Select year(dtb.tbDATE) as YearR, `CONVERT(VARCHAR(12),dtb.tbDATE,110) as DateR, (case when dtb.l_grp_no= 7 then COMPANY_B else COMPANY_A end ) as Portfolio, (case when dtb.past_days between 5 and 30 then '5-30' when dtb.past_days between 31 and 60 then'31-60' when dtb.past_days between 61 and 90 then '61-90' when dtb.past_days &gt;= 91 then '91+' when dtb.past_days &lt;5 then 'Current' else 'Dis_po' end) 'Qdel', case when lsc.SStatusC is not null and dtb.tbDATE&gt; ls.eff_date then lsc.SStatusC else 'XX'end as 'LStatus', count(dtb.refaccno) as lcount, sum (dtb.P_bal) as Lbal from sln.[dbo].[table_dtb] as dtb --left outer join sln.dbo.acct_l la on dtb.accrefno = la.accrefno left outer join (select stat_acct_l.* from sln.dbo.stat_acct_l inner join (select refaccno, max(row_id) as MaxRow_id from sln.dbo.stat_acct_l group by refaccno) as maxStatus on stat_acct_l.refaccno = maxStatus.refaccno and stat_acct_l.row_id = maxStatus.MaxRow_id) as ls on ls.refaccno = dtb.refaccno left outer join dw.dbo.AccSt_C lsc on lsc.Stat_C_ID= ls.status_code_no where dtb.l_grp_no in (7,4,8,15)and dtb.tbDATE &gt; '2010-06-31' and dtb.P_bal+dtb.l_C_bal &gt;0 group by year(dtb.tbDATE), CONVERT(VARCHAR(12),dtb.tbDATE,110), case when dtb.past_days between 5 and 30 then '5-30' when dtb.past_days between 31 and 60 then'31-60' when dtb.past_days between 61 and 90 then '61-90' when dtb.past_days &gt;= 91 then '91+' when dtb.past_days &lt;5 then 'Current' else 'Dis_po' end case when dtb.l_grp_no= 7 then COMPANY_B else COMPANY_A end, case when lsc.SStatusC is not null and dtb.tbDATE&gt; ls.eff_date Yearthen lsc.SStatusC else 'XX' end order by year(dtb.tbDATE), CONVERT(VARCHAR(12),dtb.tbDATE,110), case when dtb.l_grp_no= 7 then COMPANY_Belse COMPANY_A end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload