在使用 READ UNCOMMITTED 之前,您可能需要重新考虑您的流程。隔离交易有很多很好的理由。如果您使用 READ UNCOMMITTED 您可能仍然会得到重复,因为两个插入有可能同时检查更新并且都没有发现它们创建重复。尝试将其分成更小的批次或发出定期 COMMITS
编辑
您可以将 MainTable 更新包装在可以更快释放该表的事务中,但您仍然可能会与其他表发生冲突。
IE
BEGIN TRANSACTION
SELECT @ProcessedBit = ProcessedBit FROM MainTable WHERE ID = XXX
IF @ProcessedBit = False
UPDATE MainTable SET ProcessedBit = True WHERE ID = XXX
COMMIT TRANSACTION
IF @ProcessedBit = False
BEGIN
BEGIN TRANSACTION
-- start long running process
...
COMMIT TRANSACTION
END
编辑以启用错误恢复
BEGIN TRANSACTION
SELECT @ProcessedStatus = ProcessedStatus FROM MainTable WHERE ID = XXX
IF @ProcessedStatus = 'Not Processed'
UPDATE MainTable SET ProcessedBit = 'Processing' WHERE ID = XXX
COMMIT TRANSACTION
IF @ProcessedStatus = 'Not Processed'
BEGIN
BEGIN TRANSACTION
-- start long running process
...
IF No Errors
BEGIN
UPDATE MainTable SET ProcessedStatus = 'Processed' WHERE ID = XXX
COMMIT TRANSACTION
ELSE
ROLLBACK TRANSACTION
END