DECLARE @ UpdateDate AS TABLE ( [FlowId] [int] NOT NULL PRIMARY KEY, [UpateDate] [int] NOT NULL, [the RowId] [int] NOT NULL INDEX Idx_RowId ) The BEGIN INSERT the @ UpdateDate ([FlowId], [UpateDate], the RowId) The SELECT FlowId, UpdateDate, ROW_NUMBER () OVER (ORDER BY FlowId ASC) FROM ( The SELECT UpdateDate, MAX (FlowId) AS FlowId The FROM [_DetailIndex] WHERE PersonId=@ PersonId GROUP BY UpdateDate ) a - to perform to this @ UpdateDate table has about 20 record END
DECLARE @ Pool AS TABLE (PersonId int, WeId int, BeginDate int, EndDate int) INSERT INTO @ Pool (PersonId, WeId, BeginDate, EndDate) - if note off the INSERT, execution will soon, don't note down the execution time increases 10 times SELECT @ PersonId as PersonId, Aleem walji eId, B.U pateDate, ISNULL (C.U pateDate, 99991231) The FROM _WePool A INNER JOIN @ UpdateDate B ON A.F lowId=b. lowId LEFT the JOIN @ UpdateDate C ON B.R owId + 1=C.R owId WHERE ISNULL (C.U pateDate, 99991231) & gt; @ BeginDate AND Amy polumbo ersonId=@ PersonId Article - _WePool there are tens of millions of data, data related to @ PersonId article 30 w, made with PersonId and FlowId keyword
Now to insert data execution efficiency especially slow, @ Pool would cost more than 16 seconds, logic of _WePool reads 900 w, but if the inserts, injection time is less than 1 SEC, _WePool logical reads only 30 w, don't know what reason is this, If change the @ Pool into a temporary table, won't have this problem, If you remove the last LEFT JOIN @ UpdateDate C ON B.R owId + 1=C.R owId also won't have this problem, the great god under the guidance
CodePudding user response:
Not strange, table variables are not suitable for large amounts of data, Using a temporary table can solve calculate, don't tangle