Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A02C21337B9E for ; Mon, 28 Feb 2011 15:58:37 -0400 (AST) 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 35859-05 for ; Mon, 28 Feb 2011 19:58:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id A17F51337B39 for ; Mon, 28 Feb 2011 15:58:29 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p1SJwS1i028839; Mon, 28 Feb 2011 14:58:28 -0500 (EST) To: Craig James cc: pgsql-performance@postgresql.org Subject: Re: Query on view radically slower than query on underlying table In-reply-to: <4D6BF648.8090105@emolecules.com> References: <201102281038.04945.cousinmarc@gmail.com> <4D6B9BC9.1000404@enterprisedb.com> <4D6BE95C.3090102@emolecules.com> <27415.1298919456@sss.pgh.pa.us> <4D6BF648.8090105@emolecules.com> Comments: In-reply-to Craig James message dated "Mon, 28 Feb 2011 11:23:52 -0800" Date: Mon, 28 Feb 2011 14:58:28 -0500 Message-ID: <28838.1298923108@sss.pgh.pa.us> From: Tom Lane 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, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/540 X-Sequence-Number: 42678 Craig James writes: > Then I thought maybe putting a foreign-key constraint on table "my_version" would solve the problem: > alter table my_version add constraint fk_my_view foreign key(version_id) > references registry.version(version_id) on delete cascade; > That way, the planner would know that every key in table "my_version" has to also be in table "version", thus avoiding that part about "forcing the other join to be done in toto". But the foreign-key constraint makes no difference, it still does the full join and takes 65 seconds. That's just wishful thinking I'm afraid. The planner doesn't currently make any deductions whatsoever from the presence of a foreign key constraint; and even if it did, I'm not sure that this would help it decide that a join order constraint could safely be dropped. regards, tom lane