Quantcast
Channel: How to do an update + join in PostgreSQL? - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by lucia for How to do an update + join in PostgreSQL?

$
0
0

The first way is slower than the second way.

First:

DO $$ DECLARE   page int := 10000;  min_id bigint; max_id bigint;BEGIN  SELECT max(id),min(id) INTO max_id,min_id FROM opportunities;  FOR j IN min_id..max_id BY page LOOP     UPDATE opportunities SET sec_type = 'Unsec'    FROM opportunities AS opp    INNER JOIN accounts AS acc    ON opp.account_id = acc.id    WHERE acc.borrower = true    AND opp.sec_type IS NULL    AND opp.id >= j AND opp.id < j+page;    COMMIT;              END LOOP;END; $$;

Second:

DO $$ DECLARE   page int := 10000;  min_id bigint; max_id bigint;BEGIN  SELECT max(id),min(id) INTO max_id,min_id FROM opportunities;  FOR j IN min_id..max_id BY page LOOP    UPDATE opportunities AS opp     SET sec_type = 'Unsec'    FROM accounts AS acc    WHERE opp.account_id = acc.id    AND opp.sec_type IS NULL    AND acc.borrower = true     AND opp.id >= j AND opp.id < j+page;    COMMIT;              END LOOP;END; $$;

Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>