在存储过程中使用 while 循环是否导致 System.OutOfMemoryException?
Is use of while loop in stored procedure causing System.OutOfMemoryException?
我一直在与 System.OutOfMemoryException 作斗争。我看到了一些解决方案,但所有人都说你需要更多 RAM.I 怀疑是否是由于效率低下 code.So 让我分享我的问题。
我有 10 个不同的 table,每个都有大约 5k 条记录,我需要 select 每个 table 的一列并构建一个新的 table.I 能够插入大约 1.5k记录但随后执行停止 "System.OutOfMemoryException" 。
我的 while 循环看起来像
ALTER PROCEDURE Sp_sample
As
Select col1
into
#ControlTable
from
tab1
while exists(select * from #ControlTable)
begin
(select count(*) from #ControlTable);
select @var1 = (select top 1 col1 from #ControlTable);
select @var2 = (select top 1 col2 from table1 where col3=@var1);
if exists (select a from tablenew where col1=@var1)
begin
update tablenew set col2 = @var2 where col1 = @var1
end
else
begin
insert into tablenew values (@var1,@var2)
end
delete from #ControlTable where col1 = @var1;
end
Begin
我已经发布了示例代码以使问题更通用。
任何帮助或建议将不胜感激。
请尝试以下 while 循环并检查性能:
ALTER PROCEDURE Sp_sample
As
Select col1, ROW_NUMBER() OVER(Order By col1) AS RowNo
into
#ControlTable
from
tab1
DECLARE @Index INT=1;
DECLARE @TotalRow INT=0;
SELECT @TotalRow=COUNT(col1) FROM #ControlTable
while @Index<=@TotalRow
begin
select @var1 = var1 from #ControlTable where RowNo=@Index;
select @var2 = var2 from table1 where col1=@var1;
if exists (select a from tablenew where col1=@var1)
begin
update tablenew set col2 = @var2 where col1 = @var1
end
else
begin
insert into tablenew values (@var1,@var2)
end
SET @Index = @Index+1;
end
Begin
您可以使用 MERGE
来插入或更新 table。
Select col1, max(col2) AS col2 into #ControlTable from tab1 GROUP BY col1
MERGE tablenew AS T
USING #ControlTable AS S
ON (T.col1 = S.col1)
WHEN NOT MATCHED BY TARGET
THEN INSERT(col1, col2) VALUES(S.col1, S.col2)
WHEN MATCHED
THEN UPDATE SET T.col2 = S.col2
我一直在与 System.OutOfMemoryException 作斗争。我看到了一些解决方案,但所有人都说你需要更多 RAM.I 怀疑是否是由于效率低下 code.So 让我分享我的问题。
我有 10 个不同的 table,每个都有大约 5k 条记录,我需要 select 每个 table 的一列并构建一个新的 table.I 能够插入大约 1.5k记录但随后执行停止 "System.OutOfMemoryException" 。
我的 while 循环看起来像
ALTER PROCEDURE Sp_sample
As
Select col1
into
#ControlTable
from
tab1
while exists(select * from #ControlTable)
begin
(select count(*) from #ControlTable);
select @var1 = (select top 1 col1 from #ControlTable);
select @var2 = (select top 1 col2 from table1 where col3=@var1);
if exists (select a from tablenew where col1=@var1)
begin
update tablenew set col2 = @var2 where col1 = @var1
end
else
begin
insert into tablenew values (@var1,@var2)
end
delete from #ControlTable where col1 = @var1;
end
Begin
我已经发布了示例代码以使问题更通用。 任何帮助或建议将不胜感激。
请尝试以下 while 循环并检查性能:
ALTER PROCEDURE Sp_sample
As
Select col1, ROW_NUMBER() OVER(Order By col1) AS RowNo
into
#ControlTable
from
tab1
DECLARE @Index INT=1;
DECLARE @TotalRow INT=0;
SELECT @TotalRow=COUNT(col1) FROM #ControlTable
while @Index<=@TotalRow
begin
select @var1 = var1 from #ControlTable where RowNo=@Index;
select @var2 = var2 from table1 where col1=@var1;
if exists (select a from tablenew where col1=@var1)
begin
update tablenew set col2 = @var2 where col1 = @var1
end
else
begin
insert into tablenew values (@var1,@var2)
end
SET @Index = @Index+1;
end
Begin
您可以使用 MERGE
来插入或更新 table。
Select col1, max(col2) AS col2 into #ControlTable from tab1 GROUP BY col1
MERGE tablenew AS T
USING #ControlTable AS S
ON (T.col1 = S.col1)
WHEN NOT MATCHED BY TARGET
THEN INSERT(col1, col2) VALUES(S.col1, S.col2)
WHEN MATCHED
THEN UPDATE SET T.col2 = S.col2