Re: delete taking long time

From: "Mike Sofen" <msofen(at)runbox(dot)com>
To: "'David G(dot) Johnston'" <david(dot)g(dot)johnston(at)gmail(dot)com>, "'Andreas Kretschmer'" <andreas(at)a-kretschmer(dot)de>
Cc: "'ivo liondov'" <ivo(dot)liondov(at)gmail(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: delete taking long time
Date: 2016-03-16 01:49:25
Message-ID: 000201d17f26$13eda040$3bc8e0c0$@runbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>>From: David G. Johnston
>>Sent: Tuesday, March 15, 2016 6:25 PM

> There are around 800.000 records matching this rule, and seems to be taking
> an awful lot of time - 4 hours and counting. What could be the reason for
> such a performance hit and how could I optimise this for future cases?

the db has to touch such many rows, and has to write the transaction log. And
update every index. And it has to check the referenced tables for the
constraints. Do you have proper indexes?

Given the lack of indexes on the one table that is shown I suspect this is the most likely cause (FK + indexes)

David J.

<<

There are SEVEN FKs against that table…I would bet that’s 50% of the duration. The lack of an index, perhaps an issue, but

With that many FK references plus that many rows…the transaction log could easily blow out and start paging to disk.

When deleting more than perhaps 20k rows, I will normally write a delete loop, grabbing roughly 20-50k rows at time (server capacity

dependent), deleting that set, grabbing another set, etc. That allows the set to commit, releasing pressure on the tran log.

You can easily experiment and see how long 10k rows take to delete. If still long, the FKs are the issue…you may need to script them out,

drop them, run the deletes, then rebuild them.

Mike S.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message ivo liondov 2016-03-16 11:06:38 Re: delete taking long time
Previous Message Andreas Kretschmer 2016-03-16 01:34:52 Re: delete taking long time