From rjessil@yahoo.com Fri Jul 4 16:00:13 2008 Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id D4481650E93 for ; Fri, 4 Jul 2008 13:00:13 -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 87306-06 for ; Fri, 4 Jul 2008 13:00:09 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from web56406.mail.re3.yahoo.com (web56406.mail.re3.yahoo.com [216.252.111.85]) by postgresql.org (Postfix) with SMTP id B4518650E7B for ; Fri, 4 Jul 2008 13:00:09 -0300 (ADT) Received: (qmail 14879 invoked by uid 60001); 4 Jul 2008 16:00:08 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type:Message-ID; b=QdsdPt1e+OJv3AQtmaXsa93oL8f2v31JlKGxwtMYMkq+66wjdcaubAayVW+hIr+SyOD/2qWe5iOVycaZPQLpI5F7MGo5UzCVaBy4+GatWIsHjRcggGrWXdtdb0CwZlLUf5dhaFGNftWoaBBamO/bbN1UrnniceUhviK6qDWJo4Y=; Received: from [198.119.134.32] by web56406.mail.re3.yahoo.com via HTTP; Fri, 04 Jul 2008 09:00:06 PDT X-Mailer: YahooMailRC/1042.33 YahooMailWebService/0.7.199 Date: Fri, 4 Jul 2008 09:00:06 -0700 (PDT) From: Jessica Richard Subject: How many table scans in a delete... To: pgsql-admin@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1445676858-1215187206=:14193" Message-ID: <522587.14193.qm@web56406.mail.re3.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200807/70 X-Sequence-Number: 29680 --0-1445676858-1215187206=:14193 Content-Type: text/plain; charset=us-ascii I am just trying to understand how Postgres does its delete work. If I have a table testDad with a primary key on cola and no index on colb, and I have a kid table testKid with a foreign key to reference testDad but no index created on the foreign key column on table testKid. I have 10,000 rows in each table and I want to delete 5000 rows from the Dad table, of course , I have to kill all the kids in the Kid table first so that I won't get the constraint error. Now I am ready to run my delete command on the Dad table with the following command: delete from testDad where colb = 'abc'; (supposed select count(*) from testDad where colb = 'abc' will give me 5000 rows) Since I don't have any index on testDad.colb, I know it is going to do a table scan on the table testDad to find all the qualified rows for the delete. My question1: how many table scans will this single transaction do to find all 5000 qualified rows on the Dad table testDad? Does it scan the entire table once to get all qualified deletes? or it has to do the table scan 5000 times on testDad? then, after all the 5000 qualified rows have been found on table testDad, the constraints between the Dad and Kid table will be checked against those 5000 qualified rows on table testDad. My question 2: does it take one qualified row at a time from the Dad table then do a table scan on the kid table for constraint check? In this case, it will have to do 5000 times of table scan on the kid table. very unlikely it will scan the kid table only once to do all constraint checking for 5000 different primary values... Thanks, Jessica --0-1445676858-1215187206=:14193 Content-Type: text/html; charset=us-ascii
I am just trying to understand how Postgres does its delete work.

If I have a table testDad with a primary key on cola and no index on colb, and I have a kid table testKid with a foreign key to reference testDad but no index created on the foreign key column on table testKid.

I have 10,000 rows in each table and I want to delete 5000 rows from the Dad table, of course , I have to kill all the kids in the Kid table first so that I won't get the constraint error.

Now I am ready to run my delete command on the Dad table with the following command:

delete from testDad where colb = 'abc';

(supposed select count(*) from testDad where colb = 'abc' will give me 5000 rows)

Since I don't have any index on testDad.colb, I know it is going to do a table scan on the table testDad to find all the qualified rows for the delete.

My question1: how many table scans will this single transaction do  to find all  5000 qualified rows on the Dad table testDad? Does it scan the entire table once to get all qualified deletes? or it has to do the table scan 5000 times on testDad?

then, after all the 5000 qualified rows have been found on table testDad, the constraints between the Dad and Kid table will be checked against those 5000 qualified rows on table testDad.

My question 2: does it take one qualified row at a time from the Dad table then  do a table scan on the kid table for constraint check? In this case, it will have to do 5000 times of table scan on the kid table. very unlikely it will scan the kid table only once to do all constraint checking for 5000 different primary values...


Thanks,
Jessica



--0-1445676858-1215187206=:14193-- From scott.marlowe@gmail.com Fri Jul 4 22:24:37 2008 Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 9AC5D64FCD4 for ; Fri, 4 Jul 2008 19:24:37 -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 26461-10 for ; Fri, 4 Jul 2008 19:24:30 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from rv-out-0506.google.com (rv-out-0506.google.com [209.85.198.227]) by postgresql.org (Postfix) with ESMTP id 3EB8F64FCC4 for ; Fri, 4 Jul 2008 19:24:34 -0300 (ADT) Received: by rv-out-0506.google.com with SMTP id b25so1866404rvf.43 for ; Fri, 04 Jul 2008 15:24:32 -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:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=ixaavQgIyfN5JG7/32DpqDYFiNQJQQCqNrOf8ffDZRM=; b=BPXH7AVMfDaFWIRqo/L335AZVtgJ840ccPLRr6IlsrIDHxRgZ005Ca8cilF4t4I8eP 3BSb1mTT8EzwXqxh+wvjhzsaCpgA5rCV3yhiYQbpWJb14BZwgjkAMPdgeTPahWCxQ1XC weGWneveqUmfbVNUIR5qY3YZihmj6m/6P7zk4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=J4zJ5l/owspFXdkqOvB4/hk0fG4ee/wsmDC9OjXGH7EuHoEFTcqd2CZOzwN95rxQmN esf7sDD7Jh1QwtuogPYsH3rqc7rRiW/MChqkUN6/KhI5gKMO9WAMYF3QJEzlgW9O585v EQE8k1y13zkAv98o0F+GaOhWINS3UQpqWEMcg= Received: by 10.114.73.1 with SMTP id v1mr177449waa.166.1215210272790; Fri, 04 Jul 2008 15:24:32 -0700 (PDT) Received: by 10.114.180.17 with HTTP; Fri, 4 Jul 2008 15:24:32 -0700 (PDT) Message-ID: Date: Fri, 4 Jul 2008 16:24:32 -0600 From: "Scott Marlowe" To: "Jessica Richard" Subject: Re: How many table scans in a delete... Cc: pgsql-admin@postgresql.org In-Reply-To: <522587.14193.qm@web56406.mail.re3.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <522587.14193.qm@web56406.mail.re3.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200807/72 X-Sequence-Number: 29682 On Fri, Jul 4, 2008 at 10:00 AM, Jessica Richard wrote: > I am just trying to understand how Postgres does its delete work. > > If I have a table testDad with a primary key on cola and no index on colb, > and I have a kid table testKid with a foreign key to reference testDad but > no index created on the foreign key column on table testKid. > > I have 10,000 rows in each table and I want to delete 5000 rows from the Dad > table, of course , I have to kill all the kids in the Kid table first so > that I won't get the constraint error. Generally speaking, that's doing things wrong. It's generally easier to use a cascading delete fk so that you don't have to delete the rows from the kid table first. > Now I am ready to run my delete command on the Dad table with the following > command: > > delete from testDad where colb = 'abc'; > > (supposed select count(*) from testDad where colb = 'abc' will give me 5000 > rows) > > Since I don't have any index on testDad.colb, I know it is going to do a > table scan on the table testDad to find all the qualified rows for the > delete. > > My question1: how many table scans will this single transaction do to find > all 5000 qualified rows on the Dad table testDad? Does it scan the entire > table once to get all qualified deletes? or it has to do the table scan 5000 > times on testDad? One on table Dad. > then, after all the 5000 qualified rows have been found on table testDad, > the constraints between the Dad and Kid table will be checked against those > 5000 qualified rows on table testDad. > > My question 2: does it take one qualified row at a time from the Dad table > then do a table scan on the kid table for constraint check? In this case, > it will have to do 5000 times of table scan on the kid table. very unlikely > it will scan the kid table only once to do all constraint checking for 5000 > different primary values... Maybe. Depends on how much memory it would take to run it with various join methods. But the worst case scenario is a seq scan on the child table for each row in Dad table. Which is why it's a good idea to use indexes on FK fields.