X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1C9C7533E8 for ; Tue, 29 Mar 2005 12:31:30 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 08562-01 for ; Tue, 29 Mar 2005 11:31:25 +0000 (GMT) Received: from cmailm3.svr.pol.co.uk (cmailm3.svr.pol.co.uk [195.92.193.19]) by svr1.postgresql.org (Postfix) with ESMTP id 43E1C52952 for ; Tue, 29 Mar 2005 12:31:24 +0100 (BST) Received: from modem-2903.lion.dialup.pol.co.uk ([217.135.171.87] helo=192.168.0.102) by cmailm3.svr.pol.co.uk with esmtp (Exim 4.41) id 1DGEw7-0002VQ-3L; Tue, 29 Mar 2005 12:31:07 +0100 Subject: Re: Delete query takes exorbitant amount of time From: Simon Riggs To: Karim A Nassar , Stephan Szabo , Tom Lane Cc: Christopher Kings-Lynne , pgsql-performance@postgresql.org In-Reply-To: References: Content-Type: text/plain Organization: 2nd Quadrant Date: Tue, 29 Mar 2005 12:29:36 +0100 Message-Id: <1112095776.11750.958.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-3) Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.048 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200503/625 X-Sequence-Number: 11330 On Tue, 2005-03-29 at 01:48 -0700, Karim A Nassar wrote: > > Each value has 1/13th of the table, which is too many rows per value to > > make an IndexScan an efficient way of deleting rows from the table. > > But, the original question was that the delete that was taking a long time > was on a different table. I tried to delete 150 rows from a table with 750 > rows, which is FK referenced from this large table. If I understand > correctly, Tom suggested that the length of time was due to a sequential > scan being done on the large table for each value being deleted from the > small one. > For this FK check, there only need be one referring id to invalidate the > delete. ISTM that for any delete with a FK reference, the index could > always be used to search for a single value in the referring table > (excepting very small tables). Why then must a sequential scan be > performed in this case, and/or in general? My understanding was that you were doing a DELETE on the smaller table and that this was doing a DELETE on the measurement table because you had the FK defined as ON DELETE CASCADE. You are right - only a single row is sufficient to RESTRICT the DELETE. But for an ON UPDATE/DELETE action of CASCADE then you will want to touch all rows referenced, so a SeqScan is a perfectly valid consequence of such actions. I think now that you are using the default action, rather than specifically requesting CASCADE? Stephan, Tom: The SQL generated for RI checking by the RI triggers currently applies a limit at execution time, not at prepare time. i.e. there is no LIMIT clause in the SQL. We know whether the check will be limit 1 or limit 0 at prepare time, so why not add a LIMIT clause to the SQL so it changes the plan, not just the number of rows returned when the check query executes? (I note that PREPARE does allow you to supply a LIMIT 1 clause). That is *ought* to have some effect on the plan used by the RI check queries. In costsize.c:cost_index we would have tuples_fetched==1 and it would be hard (but not impossible) for the index cost to ever be more than the cost of a SeqScan. ...but, I see no way for OidFunctionCall8 to ever return an answer of "always just 1 row, no matter how big the relation"...so tuples_fetched is always proportional to the size of the relation. Are unique indexes treated just as very-low-selectivity indexes? - they're a very similar situation in terms of forcing an absolute, not relative, number of rows returned. Best Regards, Simon Riggs