Re: Why Does UPDATE Take So Long?

From: Bill Thoen <bthoen(at)gisnet(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Why Does UPDATE Take So Long?
Date: 2008-10-01 14:32:16
Message-ID: 48E389F0.3080203@gisnet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Many thanks to everyone who helped me with this. It'll be a while before
I understand enough to be able to do a good job of tuning my system's
configuration, but there seem to be a few basics I can apply right away.
Also pointing out how UPDATE actually works was very helpful. Since I'm
at the data building stage, most of my updates will apply to an entire
column and in cases like that it's much more efficient to simply use
joins into a new table and delete the old. In this case:

CREATE TABLE farm2 (LIKE farms);
INSERT INTO farm2 (farm_id, fips_cd, farm_nbr, prog_year) SELECT
farm_id, fips_cd, farm_nbr, '2007' FROM farms;
DROP TABLE farms;
ALTER TABLE farm2 RENAME TO farms;
CREATE UNIQUE INDEX farms_id_key ON farms(farm_id);
CREATE UNIQUE INDEX farms_fips_nbr_key ON farms(fips_cd,farm_nbr);

takes only a few minutes for this 2.77 million record table. The alternative

UPDATE farms SET prog_year='2007';

takes hours! I don't know how many because I gave up after waiting for
1.5 hrs.

Thanks all,
- Bill Thoen

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Sergio Borgonovo 2008-10-01 14:52:11 "object references" and renaming was: Why Does UPDATE Take So Long?
Previous Message Scott Marlowe 2008-10-01 14:12:59 Re: How to force PostgreSQL to use multiple cores within one connection?