Ich habe eine Tabelle mit einigen doppelten Einträgen. Ich muss alle bis auf eine ablegen und dann diese neueste aktualisieren. Ich habe versucht, mit einer temporären Tabelle und einer while-Anweisung, auf diese Weise:Wie kann ich eine Datenbank massiv aktualisieren?
CREATE TABLE #tmp_ImportedData_GenericData
(
Id int identity(1,1),
tmpCode varchar(255) NULL,
tmpAlpha3Code varchar(50) NULL,
tmpRelatedYear int NOT NULL,
tmpPreviousValue varchar(255) NULL,
tmpGrowthRate varchar(255) NULL
)
INSERT INTO #tmp_ImportedData_GenericData
SELECT
MCS_ImportedData_GenericData.Code,
MCS_ImportedData_GenericData.Alpha3Code,
MCS_ImportedData_GenericData.RelatedYear,
MCS_ImportedData_GenericData.PreviousValue,
MCS_ImportedData_GenericData.GrowthRate
FROM MCS_ImportedData_GenericData
INNER JOIN
(
SELECT CODE, ALPHA3CODE, RELATEDYEAR, COUNT(*) AS NUMROWS
FROM MCS_ImportedData_GenericData AS M
GROUP BY M.CODE, M.ALPHA3CODE, M.RELATEDYEAR
HAVING count(*) > 1
) AS M2 ON MCS_ImportedData_GenericData.CODE = M2.CODE
AND MCS_ImportedData_GenericData.ALPHA3CODE = M2.ALPHA3CODE
AND MCS_ImportedData_GenericData.RELATEDYEAR = M2.RELATEDYEAR
WHERE
(MCS_ImportedData_GenericData.PreviousValue <> 'INDEFINITO')
-- SELECT * from #tmp_ImportedData_GenericData
-- DROP TABLE #tmp_ImportedData_GenericData
DECLARE @counter int
DECLARE @rowsCount int
SET @counter = 1
SELECT @rowsCount = count(*) from #tmp_ImportedData_GenericData
-- PRINT @rowsCount
WHILE @counter < @rowsCount
BEGIN
SELECT
@Code = tmpCode,
@Alpha3Code = tmpAlpha3Code,
@RelatedYear = tmpRelatedYear,
@OldValue = tmpPreviousValue,
@GrowthRate = tmpGrowthRate
FROM
#tmp_ImportedData_GenericData
WHERE
Id = @counter
DELETE FROM MCS_ImportedData_GenericData
WHERE
Code = @Code
AND Alpha3Code = @Alpha3Code
AND RelatedYear = @RelatedYear
AND PreviousValue <> 'INDEFINITO' OR PreviousValue IS NULL
UPDATE
MCS_ImportedData_GenericData
SET
PreviousValue = @OldValue, GrowthRate = @GrowthRate
WHERE
Code = @Code
AND Alpha3Code = @Alpha3Code
AND RelatedYear = @RelatedYear
AND MCS_ImportedData_GenericData.PreviousValue ='INDEFINITO'
SET @counter = @counter + 1
END
aber es dauert zu langer Zeit, auch wenn es nur 20.000 bis 30.000 Zeilen zu verarbeiten.
Hat jemand einige Vorschläge, um die Leistung zu verbessern?
Vielen Dank im Voraus!
Verwenden Sie keine Schleife! –
Vereinbart, während TSQL Schleifen enthält, ist es nicht für sie optimiert. – MatBailie
Wenn dies Microsoft SQL Server-spezifisch ist, bitte als sqlserver markieren. Nun, entweder das oder ich gebe auf und ignoriere einfach Tag SQL. – Thomas