Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1agAdc-0005m7-M0 for pgsql-sql@arkaria.postgresql.org; Wed, 16 Mar 2016 12:33:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1agAdc-0005Aw-8S for pgsql-sql@arkaria.postgresql.org; Wed, 16 Mar 2016 12:33:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1agAcd-00045w-J0 for pgsql-sql@postgresql.org; Wed, 16 Mar 2016 12:32:19 +0000 Received: from aibo.runbox.com ([91.220.196.211]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1agAcV-0006F2-Tm for pgsql-sql@postgresql.org; Wed, 16 Mar 2016 12:32:18 +0000 Received: from [10.9.9.212] (helo=mailfront12.runbox.com) by bars.runbox.com with esmtp (Exim 4.71) (envelope-from ) id 1agAcS-0008KV-9e; Wed, 16 Mar 2016 13:32:08 +0100 Received: from cpe-76-176-177-1.san.res.rr.com ([76.176.177.1] helo=seasyslap4) by mailfront12.runbox.com with esmtpsa (uid:561468 ) (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.82) id 1agAcH-0003y5-4F; Wed, 16 Mar 2016 13:31:57 +0100 From: "Mike Sofen" To: "'Andreas Kretschmer'" , References: <503958962.9182.1458090747344.JavaMail.open-xchange@oxweb01.ims-firmen.de> <20160316115753.GA6081@tux> In-Reply-To: <20160316115753.GA6081@tux> Subject: Re: delete taking long time Date: Wed, 16 Mar 2016 05:31:45 -0700 Message-ID: <003401d17f7f$cf4f4d30$6dede790$@runbox.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQFTOZ1BdsIiUHgoP+WtPUFQHMVQTAGdHUjCAWip52sA/xZwAKA4YhJQ Content-Language: en-us X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I agree with Andreas (indexes) - 10 minutes to delete 10k rows is about 9.5 minutes too long.=20=20 Either the "select" part of the query can't find the rows quickly or the FK burden is crushing the life out of it. If every involved table has an index on their Primary Key then the 10k row delete should take maybe 30-60 seconds. Highly dependent on how many FK rows are involved. And...from a db design perspective, a table referenced by 7 FKs shouldn't be having this type of delete run against it...it's just too expensive if it must happen routinely. This is where=20 de-normalization might be called for, to collapse some of those references, or a shift to stored functions that maintain integrity versus the declared foreign keys maintaining it. Mike S. -----Original Message----- From: Andreas Kretschmer Sent: Wednesday, March 16, 2016 4:58 AM ivo liondov wrote: >=20 > explain (analyze) delete from connection where uid in (select uid from=20 > connection where ts > '2016-03-10 01:00:00' and ts < '2016-03-10=20 > 01:10:00'); >=20 >=20 > ---------------------------------------------------------------------- > ---------------------------------------------------------------------- > ----- >=20 > =A0Delete on connection=A0 (cost=3D0.43..174184.31 rows=3D7756 width=3D12= )=20 > (actual time=3D > 529.739..529.739 rows=3D0 loops=3D1) >=20 > =A0=A0 ->=A0 Nested Loop=A0 (cost=3D0.43..174184.31 rows=3D7756 width=3D1= 2) (actual=20 > time=3D > 0.036..526.295 rows=3D2156 loops=3D1) >=20 > =A0=A0 =A0 =A0 =A0 ->=A0 Seq Scan on connection connection_1=A0=20 > (cost=3D0.00..115684.55 rows=3D > 7756 width=3D24) (actual time=3D0.020..505.012 rows=3D2156 loops=3D1) >=20 > =A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 Filter: ((ts > '2016-03-10 01:00:00'::time= stamp without=20 > time > zone) AND (ts < '2016-03-10 01:10:00'::timestamp without time zone)) there is no index on the ts-column. >=20 > =A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 Rows Removed by Filter:=A03108811 >=20 > =A0=A0 =A0 =A0 =A0 ->=A0 Index Scan using connection_pkey on connection= =A0=20 > (cost=3D0.43..7.53 > rows=3D1 width=3D24) (actual time=3D0.009..0.010 rows=3D1 loops=3D2156) >=20 > =A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 Index Cond: ((uid)::text =3D (connection_1= .uid)::text) >=20 > =A0Planning time: 0.220 ms >=20 > =A0Trigger for constraint dns_uid_fkey: time=3D133.046 calls=3D2156 >=20 > =A0Trigger for constraint files_uid_fkey: time=3D39780.799 calls=3D2156 >=20 > =A0Trigger for constraint http_uid_fkey: time=3D99300.851 calls=3D2156 >=20 > =A0Trigger for constraint notice_uid_fkey: time=3D128.653 calls=3D2156 >=20 > =A0Trigger for constraint snmp_uid_fkey: time=3D59.491 calls=3D2156 >=20 > =A0Trigger for constraint ssl_uid_fkey: time=3D74.052 calls=3D2156 >=20 > =A0Trigger for constraint weird_uid_fkey: time=3D25868.651 calls=3D2156 >=20 > =A0Execution time: 165880.419 ms i guess there are no indexes for this tables and the relevant columns > I think you are right, fk seem to take the biggest chunk of time from=20 > the hole delete operation. I made a test with 10.000 rows, it took 12=20 > minutes. 20.000 rows took about 25 minutes to delete. create the missing indexes now and come back with the new duration. Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknown) Kaufbach, Saxony, Germany, Europe. N 51.05082=B0, E 13.56889=B0 --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql