The following update query is taking a long time to complete - (the longest it has run is 20 minutes before being stopped.)
However if I break the query into two parts as follows:
… it completes very quickly, each statement taking about 6 seconds each. Can anybody explain why this is. I would much rather run it as a single statement. Any suggestions?
details:
SQL Server 2000,
MailManNonProf.dbo.donations is a view containing 1, 200, 000 recs,
SelectedContactsTbl table is contains 50,000 recs,
All tables (including those under the view) are appropriately indexed.
Code:
UPDATE SelectedContactsTbl
SET SumDonations2 =
(SELECT sum(DonationValue) FROM MailManNonProf.dbo.donations d
WHERE SelectedContactsTbl.ContactID = d.contactID
)
However if I break the query into two parts as follows:
Code:
SELECT s.contactid, sum(donationvalue) dv into #table
FROM MailManNonProf.dbo.donations d, selectedcontactstbl s
WHERE d.contactid = s.contactid
GROUP BY s.contactid
UPDATE SelectedContactsTbl
SET SumDonations2 =
(SELECT sum(dv) FROM #table d
WHERE SelectedContactsTbl.ContactID = d.contactID
)
… it completes very quickly, each statement taking about 6 seconds each. Can anybody explain why this is. I would much rather run it as a single statement. Any suggestions?
details:
SQL Server 2000,
MailManNonProf.dbo.donations is a view containing 1, 200, 000 recs,
SelectedContactsTbl table is contains 50,000 recs,
All tables (including those under the view) are appropriately indexed.