考慮性能,如何在循環中將數據從一個表插入另一個表?

[英]How to insert data from one table to another in a loop considering performance?


I need to insert data from one table to another in a loop with 10k rows in each loop run.

我需要在每個循環運行中以10k行的循環中將數據從一個表插入另一個表。

I want to try this because my insert into ***() select () from ### takes so much time as source table is having some millions of records.

我想嘗試這個,因為我從###插入***()select()需要花費很多時間,因為源表有數百萬條記錄。

Will there be any improvement in performance by using this approach?

使用這種方法可以改善性能嗎?

1 个解决方案

#1


3  

You can try inserting in batches and see if there is any performance improvement. Here is the sample code:

您可以嘗試分批插入並查看是否有任何性能改進。以下是示例代碼:

DECLARE @Count INT
DECLARE @Start INT

SET @Start = 1
SELECT @Count = COUNT(*) FROM TableName1

WHILE @Start<=@Count
BEGIN
    WITH cte
    AS
    (
        SELECT Col1, Col2, Col3,ROW_NUMBER() OVER (ORDER BY Col1) AS 'RowNum' FROM TableName1
    )

    INSERT INTO TableName2 SELECT Col1, Col2, Col3 FROM cte WHERE RowNum >= @Start AND RowNum < @Start+10000

    SET @Start += 10000

    WAITFOR DELAY '00:00:10'
END

Here 10000 is the batch size and you can change this value as per your convenience.

這里10000是批量大小,您可以根據自己的方便更改此值。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/08/06/724e5a369248209a5c2f42b6933886bb.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com