I have two tables - SourceTable and DestTable - Source contains columns RowNum and Col. I want to join the two tables on Col and then update top 'n' rows of DestTable where 'n' is the value of RowNum in SourceTable. I'm able to do this with cross apply but it takes hours. I'm looking for another way of writing this query.
My Cross Apply solution is -
UPDATE t
SET dest = source
FROM #sourcetable s
CROSS apply (SELECT TOP(s.rownum) *
FROM #desttable d
WHERE d.col = s.col) t
Below is sample data -
SourceTable -
Col, Source, RowNum
11 , 111 , 2
12 , 222 , 1
DestTable (before update) -
Id, Col, Dest
1 , 11 , 0
2 , 11 , 0
3 , 11 , 0
4 , 12 , 0
5 , 12 , 0
DestTable (after update) -
Id, Col, Dest
1, 11, 111
2, 11, 111
3, 11, 0
4, 12, 222
5, 12, 0
But this performs extremely poorly (even on a properly indexed table, it takes hours when the number of rows are a few millions and columns close to 400).
Is there a more efficient way to write this query?
Update - Actual Table Structure is as follows -
DestTable (contains about 1 million records and every column is populated) - 38 datetime, 66 float, 31 int, 210 varchar(50) fields
Sourceable (it's a temp table created before the sql in question runs, contains about 100k records and every column is populated) - 16 fields total (4 int, 2 datetime, 1 float, rest varchar(50))
Indexes - I can't create a covering index that includes all of the required fields because they vary from query to query. I've tried creating indexes on various combinations of fields and found that the update runs significantly faster without any indexes.
Current test results (row counts are in DestTable) -
1000 rows (without any index) - 05:00 minutes
10000 rows (without any index) - 17:38 minutes
100000 rows (without any index) - 3 hours 7 minutes
100000 (with any index on 4 fields) - Over 7 hours when I had to stop it