Re: slow delete

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: Jessica Richard <rjessil(at)yahoo(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: slow delete
Date: 2008-07-04 05:16:31
Message-ID: 486DB22F.10106@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Jessica Richard wrote:
> I have a table with 29K rows total and I need to delete about 80K out of it.

I assume you meant 290K or something.

> I have a b-tree index on column cola (varchar(255) ) for my where clause
> to use.
>
> my "select count(*) from test where cola = 'abc' runs very fast,
>
> but my actual "delete from test where cola = 'abc';" takes forever,
> never can finish and I haven't figured why....

When you delete, the database server must:

- Check all foreign keys referencing the data being deleted
- Update all indexes on the data being deleted
- and actually flag the tuples as deleted by your transaction

All of which takes time. It's a much slower operation than a query that
just has to find out how many tuples match the search criteria like your
SELECT does.

How many indexes do you have on the table you're deleting from? How many
foreign key constraints are there to the table you're deleting from?

If you find that it just takes too long, you could drop the indexes and
foreign key constraints, do the delete, then recreate the indexes and
foreign key constraints. This can sometimes be faster, depending on just
what proportion of the table must be deleted.

Additionally, remember to VACUUM ANALYZE the table after that sort of
big change. AFAIK you shouldn't really have to if autovacuum is doing
its job, but it's not a bad idea anyway.

--
Craig Ringer

In response to

  • slow delete at 2008-07-04 00:44:40 from Jessica Richard

Browse pgsql-performance by date

  From Date Subject
Next Message Russell Smith 2008-07-04 05:30:50 Re: Define all IP's in the world in pg_hba.conf
Previous Message idc danny 2008-07-04 04:52:03 Define all IP's in the world in pg_hba.conf