我有一个 TSQL 语句需要几个小时才能运行。我确定我需要查看导入过程以避免插入重复项,但目前我只想删除所有记录,除了具有重复值的记录。ParameterValueId 是表上的主键,但我有许多重复的条目需要删除。对于每个 ParameterId、SiteId、MeasurementDateTime 和 ParameterValue,我只需要一条记录。以下是我目前删除重复记录的方法。它查找所有计数 > 1 的值。然后查找具有这些值的第一个 Id,并删除具有与这些值找到的第一个 ID 不匹配的值的所有记录。除了打印语句之外,还有一种更有效的方法来执行此操作。我可以完全使用光标来提高性能吗?
BEGIN TRANSACTION
SET NOCOUNT ON
DECLARE @BeginningRecordCount INT
SET @BeginningRecordCount =
(
SELECT COUNT(*)
FROM ParameterValues
)
DECLARE @ParameterId UNIQUEIDENTIFIER
DECLARE @SiteId UNIQUEIDENTIFIER
DECLARE @MeasurementDateTime DATETIME
DECLARE @ParameterValue FLOAT
DECLARE CDuplicateValues CURSOR FOR
SELECT
[ParameterId]
,[SiteId]
,[MeasurementDateTime]
,[ParameterValue]
FROM [ParameterValues]
GROUP BY
[ParameterId]
,[SiteId]
,[MeasurementDateTime]
,[ParameterValue]
HAVING COUNT(*) > 1
OPEN CDuplicateValues
FETCH NEXT FROM CDuplicateValues INTO
@ParameterId
,@SiteId
,@MeasurementDateTime
,@ParameterValue
DECLARE @FirstParameterValueId UNIQUEIDENTIFIER
DECLARE @DuplicateRecordsDeleting INT
WHILE @@FETCH_STATUS <> -1
BEGIN
SET @FirstParameterValueId =
(
SELECT TOP 1 ParameterValueId
FROM ParameterValues
WHERE
ParameterId = @ParameterId
AND SiteId = @SiteId
AND MeasurementDateTime = @MeasurementDateTime
AND ParameterValue = @ParameterValue
)
SET @DuplicateRecordsDeleting =
(
SELECT COUNT(*)
FROM ParameterValues
WHERE
ParameterId = @ParameterId
AND SiteId = @SiteId
AND MeasurementDateTime = @MeasurementDateTime
AND ParameterValue = @ParameterValue
AND ParameterValueId <> @FirstParameterValueId
)
PRINT 'DELETING ' + CAST(@DuplicateRecordsDeleting AS NVARCHAR(50))
+ ' records with values ParameterId : ' + CAST(@ParameterId AS NVARCHAR(50))
+ ', SiteId : ' + CAST (@SiteId AS NVARCHAR(50))
+ ', MeasurementDateTime : ' + CAST(@MeasurementDateTime AS NVARCHAR(50))
+ ', ParameterValue : ' + CAST(@ParameterValue AS NVARCHAR(50))
DELETE FROM ParameterValues
WHERE
ParameterId = @ParameterId
AND SiteId = @SiteId
AND MeasurementDateTime = @MeasurementDateTime
AND ParameterValue = @ParameterValue
AND ParameterValueId <> @FirstParameterValueId
FETCH NEXT FROM CDuplicateValues INTO
@ParameterId
,@SiteId
,@MeasurementDateTime
,@ParameterValue
END
CLOSE CDuplicateValues
DEALLOCATE CDuplicateValues
DECLARE @EndingRecordCount INT
SET @EndingRecordCount =
(
SELECT COUNT(*)
FROM ParameterValues
)
PRINT 'Beginning Record Count : ' + CAST(@BeginningRecordCount AS NVARCHAR(50))
PRINT 'Ending Record Count : ' + CAST(@EndingRecordCount AS NVARCHAR(50))
PRINT 'Total Records Deleted : ' + CAST((@BeginningRecordCount - @EndingRecordCount) AS NVARCHAR(50))
SET NOCOUNT OFF
PRINT 'RUN THE COMMIT OR ROLLBACK STATEMENT AFTER VERIFYING DATA.'
--COMMIT
--ROLLBACK