Hi All,I've been trying to make the following query more performant by breaking it up into smaller pieces. SELECT MT.A3+MT.A4 AS A34,MT.A3 -- ,M.* FROM Master_TAB M JOIN (SELECT M.A1,t3.A3,t3.A4,M.A6,M.A2,ROW_NUMBER() OVER (PARTITION BY A1,A6,A3,A4 ORDER BY A5 DESC) AS rownum FROM Master_TAB M JOIN TABle2 t2 ON M.A2=t2.A2 JOIN Table3 t3 ON t2.A2=t3.A2 ) MT ON M.A1=MT.A1 AND M.A2=MT.A2 AND MT.rownum=1ORDER BY MT.A3+MT.A4,MT.A1;I know that the Spill is caused by the Sort but I can't remove the sort (sort can't be done in front end).my master table had 1.7 million rows and almost 200 columns (bad design? I know but can't be changed as there's too much that would be affected) every row is little over 1KBhere's my attempt... -- MASTER_TAB has 1.7 million rows and 50 columns CREATE TABLE [dbo].[tmp_ABC]( [A1] [varchar](13) NOT NULL, [A2] [varchar](5) NOT NULL, [A3] [varchar](4) NOT NULL, [A4] [varchar](4) NOT NULL, [A5] [int] NULL) ON [PRIMARY]-- inserted data into tmp_ABC from master Table MASTER_TAB (PK= A1,A2,A3)create index IDX_tmp_ABC on [tmp_all_price_Target] (A1, A3, A4, A5) INCLUDE (A2)CREATE TABLE [dbo].[tmp_DEF]( [A1] [varchar](13) NOT NULL, [A2] [varchar](5) NOT NULL, [A3] [varchar](4) NOT NULL, [A4] [varchar](4) NOT NULL, [A5] [int] NULL) ON [PRIMARY] insert into tmp_DEFSelect p.A1, t.A2, p.A3, p.A4,p.A5from [tmp_ABC] tjoin (SELECT A1, A3, A4, max(A5) as A5 FROM tmp_ABC group by A1, A3, A4) p on t.A1 = p.A1 and t.A3 = p.A3 and t.A4=p.A4 and t.A5 = p.A5create index IDX_tmp_DEF on [tmp_all_UPC_Sto_Loc_for_pri] (A1,A2, A3, A4, A5)create index IDX_tmp_DEF_sort on [tmp_all_UPC_Sto_Loc_for_pri] (A3,A4) INCLUDE (A1,A2, A5)-- this is the Query that is causing the Spill (in reality I'm supposed to bring back all 200 columns fro the master table but for debug purposes I limited the columns)Select c.A3+c.A4 as A34, c.A3, c.A1 -- M.*from tmp_DEF c join MASTER_TAB M on M.A1 = c.A1 and M.A2 = c.A2order by c.A3, C.A4if I just run the following I get no spill: Select c.A3+c.A4 as A34, c.A3, c.A1 from tmp_DEF c order by c.A3, C.A4as soon as I add the Master table as a Join I get the Spill... I read many articles, tried many suggested things (creating indexes... clustered, non-clustered) without success. Maybe I'm totally in Left Field and should enhance the performance going another route? I've been breaking my head for the past 3 days and can't seem to figure it out... I had to turn for help here... Thank you in advancedJG
↧