Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id C10E29FA39B for ; Mon, 9 Apr 2007 22:30:01 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 16863-05 for ; Mon, 9 Apr 2007 22:29:57 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.245]) by postgresql.org (Postfix) with ESMTP id 54B889FA392 for ; Mon, 9 Apr 2007 22:29:57 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c24so1840822ana for ; Mon, 09 Apr 2007 18:29:55 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:in-reply-to:references:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; b=nZf078pTFutIX+9F/kFOYKmKxkMJIub54LElMELYYng3dwKl32vAEaB6WVK9IKS96RqMmkgC7OTThuMky1wfopMn1A3PDzzatciGwrLTRYOLySkoFsIFtrc3fYcUNQPsCmMs6HWBv7Y5oz5e33fCr9llAHpQFb6NDXyu3KX0QdE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:in-reply-to:references:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; b=B8ac7MuWs7UmfYRY+2mS+x6HHoikuYqvCdyuwiUdrO/s2UpsTVmU+Eclq0iJNgE60UNPVgCEpLAcNiy+XvrNVXyOfFM7j3ubEgEcoeNi55vYxXjjtWJvdcCs6zF4i1B/woWlDbAFMujiQsTwO3xGcRwRVemZi9yhPXtoZwNgua4= Received: by 10.100.132.16 with SMTP id f16mr4503562and.1176168595717; Mon, 09 Apr 2007 18:29:55 -0700 (PDT) Received: from ?17.203.35.206? ( [17.203.35.206]) by mx.google.com with ESMTP id c20sm12279534ana.2007.04.09.18.29.52; Mon, 09 Apr 2007 18:29:53 -0700 (PDT) In-Reply-To: <14019.1176155022@sss.pgh.pa.us> References: <233A9987-ABA5-4491-A304-FDA1F8FCD962@gmail.com> <14019.1176155022@sss.pgh.pa.us> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <22512E5C-A60F-43E7-A370-257F7AF956FD@gmail.com> Cc: pgsql-performance@postgresql.org Content-Transfer-Encoding: 7bit From: Drew Wilson Subject: Re: how to efficiently update tuple in many-to-many relationship? Date: Mon, 9 Apr 2007 18:29:41 -0700 To: Tom Lane X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/221 X-Sequence-Number: 24098 My apologies. That function call was some test code to verify that my subselect was only being called once. Let me try again, please. Here's the query plan for a SELECT statement that returns 1,207,161 rows in 6 seconds. MatchBox=# explain select count(translation_pair_id) from translation_pair_data join instance i using(translation_pair_id) join loc_submission ls using(loc_submission_id) where ls.is_public = true; QUERY PLAN ------------------------------------------------------------------------ ----------------------------------- Aggregate (cost=299276.72..299276.73 rows=1 width=4) -> Hash Join (cost=59962.72..294036.83 rows=2095954 width=4) Hash Cond: (i.translation_pair_id = translation_pair_data.translation_pair_id) -> Hash Join (cost=369.15..177405.01 rows=2095954 width=4) Hash Cond: (i.loc_submission_id = ls.loc_submission_id) -> Seq Scan on instance i (cost=0.00..99016.16 rows=5706016 width=8) -> Hash (cost=296.92..296.92 rows=5778 width=4) -> Index Scan using loc_submission_is_public on loc_submission ls (cost=0.00..296.92 rows=5778 width=4) Index Cond: (is_public = true) Filter: is_public -> Hash (cost=31861.92..31861.92 rows=1690292 width=4) -> Seq Scan on translation_pair_data (cost=0.00..31861.92 rows=1690292 width=4) And here's the query plan for the UPDATE query that seems to never complete. (Execution time > 30 minutes.) MatchBox=# explain update translation_pair_data set is_public = true where translation_pair_id in (select translation_pair_id from translation_pair_data join instance i using(translation_pair_id) join loc_submission ls using(loc_submission_id) where ls.is_public = true); QUERY PLAN ------------------------------------------------------------------------ ------------------------------------------------------------- Hash IN Join (cost=328000.49..453415.65 rows=1690282 width=90) Hash Cond: (public.translation_pair_data.translation_pair_id = public.translation_pair_data.translation_pair_id) -> Seq Scan on translation_pair_data (cost=0.00..31861.82 rows=1690282 width=90) -> Hash (cost=293067.74..293067.74 rows=2067660 width=8) -> Hash Join (cost=59958.35..293067.74 rows=2067660 width=8) Hash Cond: (i.translation_pair_id = public.translation_pair_data.translation_pair_id) -> Hash Join (cost=365.00..177117.92 rows=2067660 width=4) Hash Cond: (i.loc_submission_id = ls.loc_submission_id) -> Seq Scan on instance i (cost=0.00..99016.16 rows=5706016 width=8) -> Hash (cost=293.75..293.75 rows=5700 width=4) -> Index Scan using loc_submission_is_public on loc_submission ls (cost=0.00..293.75 rows=5700 width=4) Index Cond: (is_public = true) Filter: is_public -> Hash (cost=31861.82..31861.82 rows=1690282 width=4) -> Seq Scan on translation_pair_data (cost=0.00..31861.82 rows=1690282 width=4) I figure I must be doing something wrong here. Thanks for the help, Drew On Apr 9, 2007, at 2:43 PM, Tom Lane wrote: > Drew Wilson writes: >> I have 2 tables (A,B) joined in a many-to-many relationship via a >> join table ("membership"), where updating table A based on table B >> takes a very long time. >> ... >> -> Function Scan on a (cost=0.00..12.50 rows=1000 width=4) > > I think you've left out some relevant details ... there's nothing > in what you said about a set-returning function ... > > regards, tom lane