Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can anyone help me on how can i optimize my query below.
It takes more than a minute just to show 50K records.
TIA.

What I have tried:

This is my query.

with cte as
(
 select batch,lcoDt,runID,erpIO,style,article,leadtm,cfm_tag,EETA, asIO,planDt, class, pono,mainFab,matLeadTm,ExMillDt--, confirm_tag,special_range, order_qty, lpd_dt,req_del_cst
 ,ROW_NUMBER() over (partition by pono order by class) as seq
	from  (
			select distinct batch,lcoDt,runID,erpIO,style,article,leadtm,cfm_tag, 
				(case when isnull(EETA,'')<>'' and isnull(ETA,'')<>'' then ETA else EETA end) 'EETA'
				,asIO,planDt, class,pono,mainFab,matLeadTm,ExMillDt,-- confirm_tag,special_range, order_qty, lpd_dt,req_del_cst,
				 ROW_NUMBER() over (partition by poNo,class order by eeta desc) as rowNumber
			from TableMatTrack
			where class != 'TRI'
		  ) as maxDateResult
	where maxDateResult.rowNumber = 1
)
select batch 'Batch',
        lcoDt 'LCO Date', 
		runID 'Run ID', 
		asio 'AS400 IO#', 
		erpIO 'ERP IO#', 
		style 'Style ID', 
		article 'Article',
		leadtm 'Gmt LT', 
		pono 'Cust PONo',
		confirm_tag,
		special_range,
		order_qty,
		lpd_dt,
		req_del_cst,
		(dateadd(d,3,planDt)) 'PSDD Dt',
		class1_process, class1, class1_mainfab, class1_mat,class1_exMill,class1_eeta,
		class2_process, class2, class2_mainfab, class2_mat, class2_exMill,class2_eeta,
		class3_process, class3, class3_mainfab, class3_mat, class3_exMill,class3_eeta
  from
(
  select t.batch,t.lcoDt,t.runID,t.asIO,t.erpIO,t.style,t.article,t.leadtm, t.pono,t.cfm_tag, t.planDt,poDet.confirm_tag,
		poDet.special_range,
		poDet.order_qty,
		poDet.lpd_dt,
		poDet.req_del_cst,
    col = case 
     when c.col = 'class' then col+cast(seq as varchar(10))
      else 'class'+cast(seq as varchar(10))+'_'+col
    end,

    value
  from cte t left join 
				(select distinct confirm_tag,special_range, order_qty, lpd_dt,req_del_cst,order_no,IO_no
				 from TableBulkPO
				 where Approve_tag=1 and upload_tag=1) as poDet
						on poDet.order_no= t.pono and poDet.IO_no= t.asIO
  cross apply
 (
    select 'process',  case when class = 'ACC' then 'SEW' 
							when class = 'FAB' then 'CUT'
							else 'FN/PK' end 'Process' union all

	select 'class', class union all

	select 'mainfab',  case when isnull(mainFab,'') <> '' then mainFab else 'N' end union all

	select 'mat', matLeadTm union all

	select 'exMill', case when isnull(ExMillDt,'') <> '' then convert(varchar(20),ExMillDt,101) else '' end  as  ExMillDt union all

	select 'eeta', case when isnull(EETA,'') <> '' then convert(varchar(20),EETA,101) else '' end  as  EETA
	
  ) c (col, value)
) d  
pivot
(
  max(value)
  for col in (class1_process, class1, class1_mainfab, class1_mat,class1_exMill,class1_eeta,
		class2_process, class2, class2_mainfab, class2_mat, class2_exMill,class2_eeta,
		class3_process, class3, class3_mainfab, class3_mat, class3_exMill,class3_eeta)
) piv;
Posted
Updated 27-Apr-18 11:32am
v2

1 solution

Without having the whole schema and the data it's impossible to give exact solutions, but some advice:
- Use the Management studio to see the execution plan and investigate if proper indexes are used
- Also have a look if missing indexes are listed
- You have quite a lot of inline view. SQL Server has some difficulties optimizing them so try rearranging the SQL
- Use aliases in the query for all columns
- Ensure that Approve_tag and upload_tag in TableBulkPO are indexed if values of 1 represent small portion of the table
- Ensure that class is indexed if majority of the records in TableMatTrack have a value 'TRI'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900