Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WFaS0-0006KD-Sf for pgsql-performance@arkaria.postgresql.org; Tue, 18 Feb 2014 02:30:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WFaS0-0006ze-BB for pgsql-performance@arkaria.postgresql.org; Tue, 18 Feb 2014 02:30:24 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WFaRz-0006zX-3N for pgsql-performance@postgresql.org; Tue, 18 Feb 2014 02:30:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WFaRw-00024D-If for pgsql-performance@postgresql.org; Tue, 18 Feb 2014 02:30:22 +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 s1I2UE2M012234; Mon, 17 Feb 2014 21:30:14 -0500 From: Tom Lane To: Alistair Bayley cc: Jeff Janes , "pgsql-performance@postgresql.org" Subject: Re: Why is the optimiser choosing the slower query, or, understanding explain analyze output In-reply-to: References: <11385.1392687610@sss.pgh.pa.us> Comments: In-reply-to Alistair Bayley message dated "Tue, 18 Feb 2014 15:18:19 +1300" Date: Mon, 17 Feb 2014 21:30:14 -0500 Message-ID: <12233.1392690614@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) 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 Alistair Bayley writes: > On 18 February 2014 14:40, Tom Lane wrote: >> I notice though that the cost estimate for the seqscan plan isn't all that >> much lower than that for the indexscan plan. Probably lowering >> random_page_cost a bit would change the planner's mind. We have no >> information about total size of database vs available RAM, but if it's >> a mostly memory-resident database then such a change would be a good idea. > [ database size is 3GB, RAM 2GB ] The usual advice for database-in-RAM scenarios is to set random_page_cost = 1, or even to lower both random_page_cost and seq_page_cost below 1. In this case, since it's not going to be entirely RAM-resident, a compromise setting around 2 might be a good idea. > I'm particularly interested in the massive different between cost and > actual for the index plan. The seq scan plan has 451984/248694 (ratio > 1.82) for cost/actual, while the index plan has 502051/11597 (ratio > 43.29). At least the seq scan plan is only out by a factor of ~2. Most likely this means that the index plan is taking a lot more advantage of locality-of-reference than the planner is giving it credit for. I wouldn't put too much faith in those numbers by themselves though, because that's what nearly always happens if you run the same case through EXPLAIN more than once: all the data it needs is already in cache. It's a good idea to pay attention to what happens when the plan does have to read in some new data. 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