I'm trying to insert data from staging tableA to target tableB after incremental logic. TableA is staging table and has new and updated records compared to target table. Update query is running fine but the insert is taking a lot of time to run, so trying to re-write the logic.
Select
s.[column1],
s.column2
FROM
TableA S with (nolock)
LEFT JOIN TableB t with (nolock) ON
(
ISNULL(t.column1, -1) = ISNULL(s.column1, -1)
AND
ISNULL(t.[column2],-1) = ISNULL(s.[column2],-1)
)
OR
(
ISNULL(t.[column2],-1) = -1
AND
ISNULL(s.[column2],-1) <> -1
AND
ISNULL(t.[column1], -1) = ISNULL(s.column1, -1)
)
OR
(
ISNULL(t.[column1],-1) = -1
AND
ISNULL(s.[column1],-1) <> -1
AND
ISNULL(t.[column2], -1) = ISNULL(s.[column2], -1)
)
WHERE
t.column2 IS NULL
The current process is running for 3- 4 hours. I have added indexes for both columns on 2 tables, but it is not helping. How to re-write the logic without using or in join condition.
2条答案
按热度按时间j2qf4p5b1#
you can try using separate
LEFT JOIN
clauses for each condition and useWHERE
clause to filter results.Here the
WHERE
clause checks if all threeLEFT JOIN
conditions fail.⚠️keep in mind that the performance is still depends on the size of the tables, and the indexing strategy. Ensure the appropriate indexes are still in place.
hkmswyz62#
You can try rewritting using UNION ALL instead of OR condtion in LEFT JOIN as: