• That delete logic looks pretty inefficient. I think you can use this as the delete statement:

    -- Delete first 1,000 addresses

    DELETE FROM collateralmanager.DBO.ADDRESS

    WHERE ADDRESS_ID IN

    (SELECT TOP(1000) ADDRESS_ID

    FROM #tempdata

    ORDER BY ADDRESS_ID);

    -- Remove those same addresses from the temp table

    DELETE FROM #tempdata

    WHERE ADDRESS_ID IN

    (SELECT TOP(1000) ADDRESS_ID

    FROM #tempdata

    ORDER BY ADDRESS_ID);

    You also don't need a MAXDOP hint on the delete, as far as I know a delete is always executed single-threaded. (And with the single-row deletes in your version, or the 1000-row deletes in my example, you'd get a serial plan even if my memory is incorrect).

    This will allow you to drop the identity column, and subsequently the ORDER BY in the query that you are actually having trouble with. However, as mentioned before, I do not expect this to matter much since that ordering is already used by the plan for all the merge joins.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/