Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id D73A26324F1 for ; Thu, 13 May 2010 00:13:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 94599-03 for ; Thu, 13 May 2010 03:13:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from web39705.mail.mud.yahoo.com (web39705.mail.mud.yahoo.com [209.191.106.51]) by mail.postgresql.org (Postfix) with SMTP id A00F1632208 for ; Thu, 13 May 2010 00:13:44 -0300 (ADT) Received: (qmail 44439 invoked by uid 60001); 13 May 2010 03:13:42 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1273720422; bh=THilt3a8c2po3hWY275khXqaQkezvseJkMYOVbxgVSs=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=Iqy540yCoPa5oI+pOOLEDKMIpGk3K8oqeRjd59jUOoE7Z+xfy5I9Xgf98MRiQZ7iGvgCnEKKm0QGXUSKNpNp2vXq8yk+GitQg8sfK3NG4UT/CD0elh8we6wTX2TAcop/EFxJYNIUo07apz1E5J7HYfY3KFShVGcnnA7PCeW8DhA= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=b9l6VrsUxI0s3mi1ZDLdlesdP+zjr7hmurVK9Sbbv0SMr5ytVx7bcqSRAkZGtf8FClMPXkCCdpkGj8wCDTZktKulUeg1B6qKqsLQVBX2FSmK+f7d5rD2TX8D6z5IEhte9IkmnpS7hE9DLUqIVbipfgHLfVyLL1ADuowp0U1Zafk=; Message-ID: <821768.44263.qm@web39705.mail.mud.yahoo.com> X-YMail-OSG: 3UK14HIVM1kAh2rGwxIL3UXbgdg.OlxhDm4DMQEo_9q9z6p otRNXimuKKkd5cToTZMxcG2nnoG7xwc2Uwacy7Q6glk077A7bZwrO_ZsQs_s T5Ic.dHu0ik9MROsvhZCGg6r.WU6ivYYnS8CrDm7XEJqHM4M2eZCV3OTQ2ho 5oLEA72tPZAXNWC0mMgHzh56.aFU5R3GFobWYe2bwAQXhMaIlwZyKVqlYMbD h2A.Lorl0._DoGHuJsD.jyAsifLa0z_PIN34xhk8XguRAucvxArD5d5ByQGi tlIb1jAI3rWDI.m.v.rOuqy1rY.cUZvG8peWXzac.63BKa3JMKnC6IurVoNL zPCBQdZefwsPnjS1s.85B5T_VKTD0 Received: from [69.76.130.83] by web39705.mail.mud.yahoo.com via HTTP; Wed, 12 May 2010 20:13:42 PDT X-Mailer: YahooMailClassic/10.1.11 YahooMailWebService/0.8.103.269680 Date: Wed, 12 May 2010 20:13:42 -0700 (PDT) From: Bob Lunney Subject: Re: Slow Bulk Delete To: pgsql-performance@postgresql.org, thilo In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201005/60 X-Sequence-Number: 38644 Thilo,=0A=0AJust a few of thoughts off the top of my head:=0A=0A1. If you = know the ids of the rows you want to delete beforhand, insert them in a tab= le, then run the delete based on a join with this table.=0A=0A2. Better ye= t, insert the ids into a table using COPY, then use a join to create a new = table with the rows you want to keep from the first table. Drop the origin= al source table, truncate the id table, rename the copied table and add ind= exes and constraints.=0A=0A3. See if you can partition the table somehow s= o the rows you want to delete are in a single partitioned child table. Whe= n its time to delete them just drop the child table.=0A=0AOf course, if the= 1M rows you need to delete is very small compared to the total overall siz= e of the original table the first two techniques might now buy you anything= , but its worth a try.=0A=0AGood luck!=0A=0ABob Lunney=0A=0A--- On Sat, 5/8= /10, thilo wrote:=0A=0A> From: thilo =0A> Subject: [PERFORM] Slow Bulk Delete=0A> To: pgsql-performan= ce@postgresql.org=0A> Date: Saturday, May 8, 2010, 7:39 AM=0A> Hi all!=0A> = =0A> We moved from MySQL to Postgresql for some of our projects.=0A> So far= =0A> we're very impressed with the performance (especially=0A> INSERTs and= =0A> UPDATEs), except for a strange problem with the following=0A> bulk del= ete=0A> query:=0A> =0A> DELETE FROM table1 WHERE table2_id =3D ?=0A> =0A> I= went through these Wiki pages, trying to solve the=0A> problem:=0A> =0A> h= ttp://wiki.postgresql.org/wiki/SlowQueryQuestions and=0A> http://wiki.postg= resql.org/wiki/Performance_Optimization=0A> =0A> but unfortunately without = much luck.=0A> =0A> Our application is doing batch jobs. On every batch run= , we=0A> must=0A> delete approx. 1M rows in table1 and recreate these=0A> e= ntries. The=0A> inserts are very fast, but deletes are not. We cannot make= =0A> updates,=0A> because there's no identifying property in the objects of= =0A> table1.=0A> =0A> This is what EXPLAIN is telling me:=0A> =0A> EXPLAIN = ANALYZE DELETE FROM table1 WHERE table2_id =3D=0A> 11242939=0A> =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0=0A> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=0A> =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0=0A> =A0 =A0 =A0 =A0=A0=A0QUERY=0A> PLAN=0A> -----------= ---------------------------------------------------------------------------= --------------------------------------=0A> Index Scan using sr_index on ta= ble1=A0 (cost=3D0.00..8.56=0A> rows=3D4 width=3D6)=0A> (actual time=3D0.111= ..0.154 rows=3D4 loops=3D1)=0A> =A0=A0=A0Index Cond: (table2_id =3D 1124293= 9)=0A> Total runtime: 0.421 ms=0A> (3 rows)=0A> =0A> This seems to be very= fast (using the index), but running=0A> this query=0A> from JDBC takes up = to 20ms each. For 1M rows this sum up to=0A> several=0A> hours. When I have= a look at pg_top psql uses most of the=0A> time for the=0A> deletes. CPU u= sage is 100% (for the core used by=0A> postgresql). So it=0A> seems that po= stgresql is doing some sequential scanning or=0A> constraint=0A> checks.=0A= > =0A> This is the table structure:=0A> =0A> id=A0=A0=A0=0A> bigint=A0=A0= =A0=A0=A0(primary key)=0A> table2_id=A0=A0=A0=0A> bigint=A0=A0=A0=A0=A0(for= eign key constraint=0A> to table 2, *indexed*)=0A> table3_id=A0=A0=A0=0A> b= igint=A0=A0=A0=A0=A0(foreign key constraint=0A> to table 3, *indexed*)=0A> = some non-referenced text and boolean fields=0A> =0A> My server settings (Po= tgresql 8.4.2):=0A> =0A> shared_buffers =3D 1024MB=0A> effective_cache_size= =3D 2048MB=0A> work_mem =3D 128MB=0A> wal_buffers =3D 64MB=0A> checkpoint_= segments =3D 32=0A> checkpoint_timeout =3D 15min=0A> checkpoint_completion_= target =3D 0.9=0A> =0A> It would be very nice to give me a hint to solve th= e=0A> problem. It=0A> drives me crazy ;-)=0A> =0A> If you need more details= please feel free to ask!=0A> =0A> Thanks in advance for your help!=0A> =0A= > Kind regards=0A> =0A> Thilo=0A> =0A> -- =0A> Sent via pgsql-performance m= ailing list (pgsql-performance@postgresql.org)=0A> To make changes to your = subscription:=0A> http://www.postgresql.org/mailpref/pgsql-performance=0A> = =0A=0A=0A