Re: Delete performance

From: Greg Smith <greg(at)2ndQuadrant(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Delete performance
Date: 2011-06-01 06:14:11
Message-ID: 4DE5D8B3.1070403@2ndQuadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On 05/30/2011 08:08 PM, Jarrod Chesney wrote:
> My database uses joined table inheritance and my server version is 9.0
> I have about 120,000 records in the table that everything else inherits from, if i truncate-cascaded this table it happens almost instantly. If i run 30,000 prepared "DELETE FROM xxx WHERE "ID" = ?" commands it takes close to 10 minutes.
>
> My foreign keys to the base table are all set with "ON DELETE CASCADE".

You may also want to make them DEFERRABLE and then use "SET CONSTRAINTS
ALL DEFERRABLE" so that the constraint checking all happens at one
time. This will cause more memory to be used, but all the constraint
related work will happen in a batch.

You mentioned inheritance. That can cause some unexpected problems
sometimes. You might want to do:

EXPLAIN DELETE FROM ...

To see how this is executing. EXPLAIN works fine on DELETE statements,
too, and it may highlight something strange about how the deletion is
happening. If you can, use EXPLAIN ANALYZE, but note that this will
actually execute the statement--the deletion will happen, it's not just
a test.

There may be a problem with the query plan for the deletion that's
actually causing the issue here, such as missing the right indexes. If
you have trouble reading it, http://explain.depesz.com/ is a good web
resources to help break down where the time is going.

--
Greg Smith 2ndQuadrant US greg(at)2ndQuadrant(dot)com Baltimore, MD
PostgreSQL Training, Services, and 24x7 Support www.2ndQuadrant.us
"PostgreSQL 9.0 High Performance": http://www.2ndQuadrant.com/books

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Reuven M. Lerner 2011-06-01 09:19:30 Re: Speeding up loops in pl/pgsql function
Previous Message Tom Lane 2011-06-01 03:47:42 Re: [PERFORM] Hash Anti Join performance degradation