Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V5MJx-0005En-7S for pgsql-performance@arkaria.postgresql.org; Fri, 02 Aug 2013 20:51:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V5MJw-0000JM-K5 for pgsql-performance@arkaria.postgresql.org; Fri, 02 Aug 2013 20:51:32 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V5MJv-0000JF-EP for pgsql-performance@postgresql.org; Fri, 02 Aug 2013 20:51:31 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V5MJo-0002T7-By for pgsql-performance@postgresql.org; Fri, 02 Aug 2013 20:51:30 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id r72KpNrR008361; Fri, 2 Aug 2013 16:51:23 -0400 From: Tom Lane To: Scott Marlowe cc: "pgsql-performance@postgresql.org" Subject: Re: subselect requires offset 0 for good performance. In-reply-to: References: <4522.1375400642@sss.pgh.pa.us> <6895.1375471878@sss.pgh.pa.us> Comments: In-reply-to Scott Marlowe message dated "Fri, 02 Aug 2013 14:26:09 -0600" Date: Fri, 02 Aug 2013 16:51:23 -0400 Message-ID: <8360.1375476683@sss.pgh.pa.us> X-Pg-Spam-Score: -3.4 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Scott Marlowe writes: > Yep. Added the indexes and performance went right into the dumper. New > plan on new table with old data added in random order now looks like > the old table, only worse because it's on a slower drive. Just to be > complete here's the plan: http://explain.depesz.com/s/PYH Note that I > created new table with order by random() and created indexes. Ran > analyze on it, and the select plan looks similar now: > http://explain.depesz.com/s/bsE > So maybe I can make a test case now. But to summarize, when it can use > indexes this query gets REAL slow because it lacks a materialize step. > That seem about right? Well, the plans shown here could *not* use a materialize step because the inner scan makes use of a value from the current outer row. The materialized plan has to omit one of the index conditions from the inner scan and then apply it as a join condition. I suspect the real reason that the fast case is fast is that the inner relation, even without the p.tree_sortkey >= pro_partners.tree_sortkey condition, is empty, and thus the join runs very quickly. But the planner doesn't know that. Its estimate of the row count isn't very large, but it's definitely not zero, plus it thinks that adding the additional index condition reduces the rowcount by a factor of 3 from there. So it comes to the wrong conclusion about the value of materializing a fixed inner relation as opposed to using a parameterized indexscan. Have you tried increasing the statistics targets for these join columns? It's also possible that what you need to do is adjust the planner's cost parameters ... regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance