I have a Customer table with 200.000+ records which i have to loop through and check for a condition(on a particular column) and update the "IsPremiumUser" Column to TRUE. what is the best way to go about it?
I see i can achieve this in two ways
- Use Join and Update
- Loop through each record and update it.
I am using SQL Server2008.
Update: I did not mean looping through records using C# or Java but in the SQL Itself. I am looking for ways to reduce the execution time as there are more than 200.000 records. It is taking 20 mins using update+join
Update2: Here are the queries.
MERGE pref.personpreference AS T
USING (SELECT PR.PersonID,PR.PersonroleId from personrole PR ) as S
ON (T.PersonRoleID = S.PersonRoleID)
WHEN MATCHED THEN
UPDATE SET T.SDRPERSONID = S.PersonId;
UPDATE PP
SET PP.SDRPersonID = PR.PersonID
FROM PREF.PersonPreference PP JOIN PersonRole PR
ON PP.PersonRoleID = PR.PersonRoleID
Notes: Merge is taking 17 mins and update 20 mins to execute. But i see the number of records modified differ by some 1000 between these statements.
How many records is it updating?Just found out what [2Lakh][1] is – Brent D Aug 01 '11 at 18:44