0

I have a table with about 20 columns. It currently holds 8 million rows of data. I need to update one NULL column for all 8 million rows, it is a GUID column and a FK for another table column.

Currently, I am running simple update query in a loop. But it is taking way too long.

DO $$
BEGIN
   FOR counter IN 1..1000 LOOP

        UPDATE "Shops"
         SET "Country" = '410006e1-ca4e-4502-a9ec-e54d922d2c00'
         FROM (SELECT "Id"
                  FROM "Shops"
                   WHERE "Country" IS NULL LIMIT 1000) 
         AS "part"
         WHERE "Shops"."Id" = "part"."Id";

         COMMIT;
      RAISE NOTICE 'Counter: %', counter;

   END LOOP;
END; $$
7
  • Is GUID a fixed value? Commented Dec 28, 2020 at 8:57
  • 4
    Why don't you directly update the table without loop? Commented Dec 28, 2020 at 9:01
  • 3
    Updating 8 million rows shouldn't take 6 hours. Most probably your UPDATE was waiting for a lock due to other concurrent modifications. Doing that in batches won't make those locks go away and is typically slower than one large transaction Commented Dec 28, 2020 at 10:17
  • 3
    Completely unrelated to your problem, but: you should really avoid those dreaded quoted identifiers. They are much more trouble than they are worth it. wiki.postgresql.org/wiki/… Commented Dec 28, 2020 at 10:17
  • 1
    How slow is it? Do an explain (analyze, buffers) on your query to see why it is so slow. Commented Dec 28, 2020 at 19:49

1 Answer 1

1

Updating 8 million rows should not take 6 hours. But it can take a long time. It is often faster to just repopulate the table:

create table temp_shops as
    select . . . ,
           '410006e1-ca4e-4502-a9ec-e54d922d2c00' as country,
            . . . 
    from shops
    where country is null;

Once you have tested this to be sure it does what you want, you can truncate the table and insert the values:

truncate table shops;  -- be careful.  This empties the table but you have a copy!

insert into shops ( . . . )
    select *
    from temp_shops;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.