使用大量插入语句优化大型SP

时间:2015-01-20 02:02:03

标签: sql sql-server tsql optimization insert

我有一个存储过程,可以在一个表上执行大约50个插入语句。我遇到了创建大量数据库和大型日志文件的问题。

我已经看到过将脚本拆分成批处理的参考资料,但我并不完全确定如何做到这一点。

是否在每次插入或BEGIN END语句后放置GO命令?
还是有另一种我不知道的方法。

我正在使用SQL Server 2012。

示例:

`

-- E1.005
INSERT INTO [Results] ([Table], EstabID, ID, PersonID, EpiNo, ProdType,
    [ErrorLevel], [ErrorCode], [RowID], [QueryField], [ValCode], [ValRule],[ErrorMessage])
SELECT
    'A' AS [Table], 
    A.EstabID, 
    A.ID, 
    A.PersonID, 
    A.EpiNo, 
    A.ProdType,
    'Critical' AS [ErrorLevel], 
    'E1.005' AS [ErrorCode],
    A.RowID,
    'EstabID' AS [QueryField], 
    'If EstabID second position (starting from left) is not in (''1'',''2'',''4'',''5'') then return Error Code' AS [ValCode], 
    'Critical Error: If establishment sector not 1, 2, 4 or 5.' AS [ValRule], 
    'Critical Error: The Establishment ID character in the second position of this field (which indicates the associated establishment sector) needs to be either 1,2,4 or 5 for this to be a valid code' 
        AS [ErrorMessage]
FROM  tmp_A A
LEFT OUTER JOIN Results B ON A.RowID = B.RowID AND LEFT(B.ErrorCode,2) IN ('E1','W1')
WHERE ISNULL(A.ID,'') <> '' and SUBSTRING(A.ID,2,1) not in ('1','2','4','5') AND B.RowID IS NULL;

-- E1.006
INSERT INTO [Results] ([Table], EstabID, ID, PersonID, EpiNo, ProdType,
    [ErrorLevel], [ErrorCode], [RowID], [QueryField], [ValCode], [ValRule],[ErrorMessage])
SELECT
    'A' AS [Table], 
    A.EstabID, 
    A.ID, 
    A.PersonID, 
    A.EpiNo, 
    A.ProdType,
    'Critical' AS [ErrorLevel], 
    'E1.006' AS [ErrorCode],
    A.RowID,
    'EstabID' AS [QueryField], 
    'If EstabID third and fourth position (starting from left) is blank or not zero filled then return Error Code' AS [ValCode], 
    'Critical Error: If region is not zero filled.' AS [ValRule], 
    'Critical Error: The Establishment ID characters in the 3rd and 4th position of the string (which identify the region code - a two character string) zero filled where no specific region identifier forms part of the state establishment identifier.' 
        AS [ErrorMessage]
FROM  tmp_A A
LEFT OUTER JOIN Results B ON A.RowID = B.RowID AND LEFT(B.ErrorCode,2) IN ('E1','W1')
WHERE ISNULL(A.EstabID,'') <> '' and (SUBSTRING(A.EstabID,3,1) = ' ' OR SUBSTRING(A.EstabID,4,1) = ' ') AND B.RowID IS NULL;

-- E1.007
INSERT INTO [Results] ([Table], EstabID, ID, PersonID, EpiNo, ProdType,
    [ErrorLevel], [ErrorCode], [RowID], [QueryField], [ValCode], [ValRule],[ErrorMessage])
SELECT
    'A' AS [Table], 
    A.EstabID, 
    A.ID, 
    A.PersonID, 
    A.EpiNo, 
    A.ProdType,
    'Critical' AS [ErrorLevel], 
    'E1.007' AS [ErrorCode],
    A.RowID,
    'EstabID' AS [QueryField], 
    'If EstabID in positions 5 to 9 (starting from left) does not have a length of 5 then return an error.  If EstabID in positions 5 to 9 (starting from left) is blank or null then return error code' AS [ValCode], 
    'Critical Error: If organisation is not 5 characters long and/or zero filled.  This can be alphanumeric' AS [ValRule], 
    'Critical Error: The Establishment ID characters in positions 5 to 9 of the string (which identify the establishment - a five character alphanumeric string) need to be left justified and zero filled.  Where a fully numeric identifier is used in the jurisdiction this part of the string must be five characters in length.' 
        AS [ErrorMessage]
FROM  tmp_A A
LEFT OUTER JOIN Results B ON A.RowID = B.RowID AND LEFT(B.ErrorCode,2) IN ('E1','W1')
WHERE ISNULL(A.EstabID,'') <> '' AND (SUBSTRING(A.EstabID,5,1) = ' ' OR SUBSTRING(A.EstabID,6,1) = ' ' OR
    SUBSTRING(A.EstabID,7,1) = ' ' OR SUBSTRING(A.EstabID,8,1) = ' ' OR SUBSTRING(A.EstabID,9,1) = ' ') AND B.RowID IS NULL;`

0 个答案:

没有答案