Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7A2921EB83EA for ; Thu, 23 Feb 2012 10:25:47 -0400 (AST) Received: from relay3-d.mail.gandi.net ([217.70.183.195]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S0Zc9-0006PL-VL for pgsql-performance@postgresql.org; Thu, 23 Feb 2012 14:25:47 +0000 X-Originating-IP: 217.70.178.135 Received: from mfilter6-d.gandi.net (mfilter6-d.gandi.net [217.70.178.135]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id 9BFD4A80D9; Thu, 23 Feb 2012 15:25:18 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter6-d.gandi.net Received: from relay3-d.mail.gandi.net ([217.70.183.195]) by mfilter6-d.gandi.net (mfilter6-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id 7TsTVj2eTsD7; Thu, 23 Feb 2012 15:25:17 +0100 (CET) X-Originating-IP: 98.27.54.176 Received: from [192.168.10.146] (cpe-098-027-054-176.nc.res.rr.com [98.27.54.176]) (Authenticated sender: adsend@dunslane.net) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 63DE5A80CE; Thu, 23 Feb 2012 15:25:16 +0100 (CET) Message-ID: <4F464C49.4050503@dunslane.net> Date: Thu, 23 Feb 2012 09:25:13 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20120131 Thunderbird/10.0 MIME-Version: 1.0 To: =?UTF-8?B?TWFyY2luIE1hxYRr?= CC: "Reuven M. Lerner" , pgsql-performance@postgresql.org Subject: Re: Very long deletion time on a 200 GB database References: <4F45FB55.1020600@lerner.co.il> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/160 X-Sequence-Number: 46332 On 02/23/2012 05:07 AM, Marcin Ma=C5=84k wrote: >> DELETE FROM B >> WHERE r_id IN (SELECT R.id >> FROM R, B >> WHERE r.end_date< (NOW() - (interval '1 day' * 30)) >> AND r.id =3D b.r_id >> > How about: > > DELETE FROM B > WHERE r_id IN (SELECT distinct R.id > FROM R WHERE r.end_date< (NOW() - (interval '1 day' * 30)) > > ? > Or possibly without the DISTINCT. But I agree that the original query=20 shouldn't have B in the subquery - that alone could well make it crawl. What is the distribution of end_dates? It might be worth running this in=20 several steps, deleting records older than, say, 90 days, 60 days, 30 day= s. cheers andrew