Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id C1564C76A5C for ; Sun, 26 Feb 2012 17:00:44 -0400 (AST) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S1lCx-00051O-2k for pgsql-performance@postgresql.org; Sun, 26 Feb 2012 21:00:43 +0000 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 q1QL0LpJ016099; Sun, 26 Feb 2012 16:00:21 -0500 (EST) To: Mark Hills cc: pgsql-performance@postgresql.org Subject: Re: Index condition in a Nested Loop In-reply-to: References: Comments: In-reply-to Mark Hills message dated "Sun, 26 Feb 2012 14:16:47 +0000" Date: Sun, 26 Feb 2012 16:00:20 -0500 Message-ID: <16098.1330290020@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/220 X-Sequence-Number: 46392 Mark Hills writes: > What is that prevents the index condition from being used in earlier parts > of the query? Only where a single condition is present is it be used below > the final join. "WHERE job.jid IN (1234)" is simplified to "WHERE job.jid = 1234", and that in combination with "JOIN ON job.jid = middle.jid" allows deduction of "middle.jid = 1234" a/k/a "task.jid = 1234", leading to the recognition that only one row from "task" is needed. There is no such transitive propagation of general IN clauses. The problem with your slower queries is not that they're using merge joins, it's that there's no scan-level restriction on the task table so that whole table has to be scanned. Another thing that's biting you is that the GROUP BY in the view acts as a partial optimization fence: there's only a limited amount of stuff that can get pushed down through that. You might consider rewriting the view to avoid that, along the lines of create view middle2 as SELECT task.jid, task.tid, (select count(resource.name) from resource where task.tid = resource.tid) AS nresource FROM task; This is not perfect: this formulation forces the system into essentially a nestloop join between task and resource. In cases where you actually want results for a lot of task rows, that's going to lose badly. But in the examples you're showing here, it's going to work better. regards, tom lane