从表格到另一个表格的批量插入

时间:2013-06-07 02:02:04

标签: sql-server bulkinsert

我们在SQL Server 2008 R2中有2个表。我们必须定期插入表A到表B的一批记录。在插入时,表B仍然可以选择SELECT& UPDATE。目前,我们使用INSERT..SELECT从表A复制到表B.但问题是在插入时,有时会导致UPDATE语句到TABLE B超时。

是否有更好的批量插入解决方案从表到另一个不会导致阻塞?

2 个答案:

答案 0 :(得分:0)

1将事务超时设置为足够大的值,以使该语句不再处于超时状态。

2使用CURSOR并逐行进行

3尝试这种做事方式。需要行标识符(例如IDENTITY),最好在该字段上有PK或INDEX:

SET NOCOUNT ON;

CREATE TABLE #A(
    row_id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    data INT NOT NULL
);

CREATE TABLE #B(
    row_id INT NOT NULL PRIMARY KEY,
    data INT NOT NULL
);

-- TRUNCATE TABLE #B; -- no truncate needed since you just want to add rows, not copy the whole table

DECLARE @batch_size INT;
SET @batch_size = 10000;

DECLARE @from_row_id INT;
DECLARE @to_row_id INT;

-- You would use this to establish the first @from_row_id if you wanted to copy the whole table
-- SELECT
--   @from_row_id=ISNULL(MIN(row_id),-1)
-- FROM
--   #A AS a;

SELECT
    @from_row_id=ISNULL(MAX(row_id),-1)
FROM
    #B AS b;

IF @from_row_id=-1
    SELECT
        @from_row_id=ISNULL(MIN(row_id),-1)
    FROM
        #A AS a;
ELSE
    SELECT
        @from_row_id=ISNULL(MIN(row_id),-1)
    FROM
        #A AS a
    WHERE
        row_id>@from_row_id;

WHILE @from_row_id>=0
BEGIN
    SELECT
        @to_row_id=ISNULL(MAX(row_id),-1)
    FROM
        (
            SELECT TOP(@batch_size)
                row_id
            FROM
                #A AS a
            WHERE
                row_id>=@from_row_id
        ) AS row_ids

    IF @to_row_id=-1
    BEGIN
        INSERT 
            #B
        SELECT
            *
        FROM
            #A AS a
        WHERE
            row_id>=@from_row_id;

        BREAK;
    END
    ELSE
        INSERT 
            #B
        SELECT
            *
        FROM
            #A AS a
        WHERE
            row_id BETWEEN @from_row_id AND @to_row_id;

    SELECT
        @from_row_id=ISNULL(MIN(row_id),-1)
    FROM
        #A AS a
    WHERE
        row_id>@to_row_id;
END 

DROP TABLE #B;
DROP TABLE #A;

答案 1 :(得分:0)

他们最明显的解决方案是使用Stanley建议的小批量产品。如果这不是一个选项,您可以探索“(transaction level) snapshot isolation