I have the following UPDATE scenario:
UPDATE destTable d
SET d.test_count = ( SELECT COUNT( employee_id )
FROM sourceTable s
WHERE d.matchCode1 = s.matchCode1 AND
d.matchCode2 = s.matchCode2 AND
d.matchCode3 = s.matchCode3
GROUP BY matchCode1, matchCode2, matchCode3, employee_id )
I have to execute this in a loop changing out the match codes for each iteration.
Between two large tables (~500k records each), this query takes an unacceptably long time to execute. If I just had to execute it once, I wouldn't care too much. Given it is being executed about 20 times, it takes way too long for my needs.
It requires two full table scans (one for the destTable and another for the subquery).
Questions:
What techniques do you recommend to speed this up?
Does the SQL-optimizer run the subquery for each row I'm updating in the destTable to satisfy the where-clause of the subquery or does it have some super intelligence to do this all at once?