Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 62A1C37BFC5 for ; Thu, 2 Oct 2008 06:42:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 48811-08 for ; Thu, 2 Oct 2008 06:42:22 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.236]) by postgresql.org (Postfix) with ESMTP id EF2D237BA3C for ; Thu, 2 Oct 2008 06:42:20 -0300 (ADT) Received: by wr-out-0506.google.com with SMTP id c49so428847wra.19 for ; Thu, 02 Oct 2008 02:42:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type; bh=5Knn5J+kP4vUNolbcakGDWM5oKF06VILTtijBm4uK7Y=; b=T0kdosucjm9rQxuekzwNesAWPzp71K1vX1OPg2vOe4YR7iQN7BdpAvn4zdhckYHdDp zjTRnISXOjvIA53XKB4LUhzLGAKLtK6IbBJrQYdFrm8uloG94e03XziN/VPrSStvpdET GIHcgFcxKi4iDRoeJZkao/nBf8b9mAKMrd7BM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=NCVBef3lNQ+0TK6jUjZMqo1K/LnVDoNtE9zz+uulWzwOn13H9eCBV3ftuf+r1+lohW Rwlb29l+c1boK8/sSOUrdi6eNvOmteUbNX6nbbotjtdQxG6MejEDVNpRVbFfEFbRp0Ow pjrhYhAyYZ+mG5EsRpnsrlcVC/xJH5m4fM9mc= Received: by 10.65.250.11 with SMTP id c11mr16327799qbs.17.1222940536040; Thu, 02 Oct 2008 02:42:16 -0700 (PDT) Received: by 10.65.51.18 with HTTP; Thu, 2 Oct 2008 02:42:15 -0700 (PDT) Message-ID: <331e40660810020242u2f0f5de2qe146312e448a7df4@mail.gmail.com> Date: Thu, 2 Oct 2008 12:42:15 +0300 From: "=?ISO-8859-5?B?svbi0Nv22SDC2Nzn2OjY3Q==?=" To: pgsql-performance@postgresql.org Subject: Delete performance again MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_909_27631440.1222940536035" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200810/16 X-Sequence-Number: 31597 ------=_Part_909_27631440.1222940536035 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello. I have a database with company table that have a bunch of related (delete=cascade) tables. Also it has 1<->M relation to company_descr table. Once we've found that ~half of our companies do not have any description and we would like to remove them. First this I've tried was delete from company where id not in (select company_id from company_descr); I've tried to analyze command, but unlike to other RDBM I've used it did not include cascade deletes/checks into query plan. That is first problem. It was SLOW. To make it faster I've done next thing: create temporary table comprm(id) as select id from company; delete from comprm where id in (select company_id from company_descr); delete from company where id in (select id from comprm); That was much better. So the question is why postgresql can't do such a thing. But it was better only until "removing" dataset was small (~5% of all table). As soon as I've tried to remove 50% I've got speed problems. I've ensured I have all indexes for both ends of foreign key. I've tried to remove all cascaded entries by myself, e.g.: create temporary table comprm(id) as select id from company; delete from comprm where id in (select company_id from company_descr); delete from company_alias where company_id in (select id from comprm); ... delete from company where id in (select id from comprm); It did not help until I drop all constraints before and recreate all constraints after. Now I have it work for 15minutes, while previously it could not do in a day. Is it OK? I'd say, some (if not all) of the optimizations could be done by postgresql optimizer. ------=_Part_909_27631440.1222940536035 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
Hello.

I have a database with company table that have a bunch of related (delete=cascade) tables.
Also it has 1<->M relation to company_descr table.
Once we've found that ~half of our companies do not have any description and we would like to remove them.
First this I've tried was
delete from company where id not in (select company_id from company_descr);
I've tried to analyze command, but unlike to other RDBM I've used it did not include cascade deletes/checks into query plan. That is first problem.
It was SLOW. To make it faster I've done next thing:

create temporary table comprm(id) as select id from company;
delete from comprm where id in (select company_id from company_descr);
delete from company where id in (select id from comprm);

That was much better. So the question is why postgresql can't do such a thing.
But it was better only until "removing" dataset was small (~5% of all table).
As soon as I've tried to remove 50% I've got speed problems. I've ensured I have all indexes for both ends of foreign key.
I've tried to remove all cascaded entries by myself, e.g.:

create temporary table comprm(id) as select id from company;
delete from comprm where id in (select company_id from company_descr);
delete from company_alias where company_id in (select id from comprm);
...
delete from company where id in (select id from comprm);

It did not help until I drop all constraints before and recreate all constraints after.
Now I have it work for 15minutes, while previously it could not do in a day.

Is it OK? I'd say, some (if not all) of the optimizations could be done by postgresql optimizer.

------=_Part_909_27631440.1222940536035--