Sample Header Ad - 728x90

Union does not always eliminate duplicates

17 votes
1 answer
1712 views
I have the following query and expect that as a result I will have a list of IDs without duplicates. But sometimes it produces duplicates (1-2 on 4 million rows). Why can it happen? I run it with the default (read committed) isolation level. I can't use tablock/serializible because these are OLTP tables with hundreds of changes. ``` CREATE TABLE #characterId ( CharacterId BIGINT ) DECLARE @dateTimeFrom DATETIME = '2025-05-04' , @dateTimeTo DATETIME = '2025-05-07' INSERT INTO #characterId (CharacterId) SELECT Id FROM table1 u WHERE u.DateUpdated >= @dateTimeFrom AND u.DateUpdated = @dateTimeFrom AND usi.DateUpdated = @dateTimeFrom AND ust.DateCreated (Build 20348: ) (Hypervisor) Plan with UNION ALL instead of first UNION: enter image description here
Asked by Novitskiy Denis (331 rep)
May 6, 2025, 07:33 AM
Last activity: May 8, 2025, 06:09 PM