For performance, which option would be better for large datasets that need to be updated?
Using a CASE statement or individual update requests?
CASE example:
UPDATE tbl_name SET field_name = CASE WHEN condition_1 THEN 'Blah' WHEN condition_2 THEN 'Foo' WHEN condition_x THEN 123 ELSE 'bar' END AS value
Individual request example:
UPDATE tbl_name SET field_name = 'Blah' WHERE field_name = condition_1 UPDATE tbl_name SET field_name = 'Foo' WHERE field_name = condition_2 UPDATE tbl_name SET field_name = 123 WHERE field_name = condition_x UPDATE tbl_name SET field_name = 'bar' WHERE field_name = condition_y
NOTE. About 300,000 records will be updated, and the CASE statement will have about 10,000 WHEN clauses. If you use individual requests, it is also about 10,000.
performance sql sql-update postgresql case
Phill pafford
source share