From: Heikki Linnakangas <heikki@enterprisedb.com>
To: jay <jackem.mojx@alibaba-inc.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Postgresql update op is very very slow
Date: Wed, 25 Jun 2008 13:11:02 +0300
Message-ID: <486219B6.7050003@enterprisedb.com> (raw)
In-Reply-To: <004b01c8d671$3dec1630$0544000a@hz.ali.com>
References: <473587.97248.qm@web32701.mail.mud.yahoo.com>
<op.uc83c7m2cigqcu@apollo13.peufeu.com>
<004b01c8d671$3dec1630$0544000a@hz.ali.com>
jay wrote:
> I've a table with about 34601755 rows ,when I execute 'update msg_table set
> type=0;' is very very slow, cost several hours, but still not complete?
>
> Why postgresql is so slowly? Is the PG MVCC problem?
Possibly. Because of MVCC, a full-table update will actually create a
new version of each row.
I presume that's a one-off query, or a seldom-run batch operation, and
not something your application needs to do often. In that case, you
could drop all indexes, and recreate them after the update, which should
help a lot:
BEGIN;
DROP INDEX <index name>, <index name 2>, ...; -- for each index
UPDATE msg_table SET type = 0;
CREATE INDEX ... -- Recreate indexes
COMMIT;
Or even better, instead of using UPDATE, do a SELECT INTO a new table,
drop the old one, and rename the new one in its place. That has the
advantage that the new table doesn't contain the old row version, so you
don't need to vacuum right away to reclaim the space.
Actually, there's an even more clever trick to do roughly the same thing:
ALTER TABLE msg_table ALTER COLUMN type TYPE int4 USING 0;
(assuming type is int4, replace with the actual data type if necessary)
This will rewrite the table, similar to a DROP + CREATE, and rebuild all
indexes. But all in one command.
--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: heikki@enterprisedb.com, jackem.mojx@alibaba-inc.com
Subject: Re: Postgresql update op is very very slow
In-Reply-To: <486219B6.7050003@enterprisedb.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox