Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8u2u-0001gC-Ur for pgsql-performance@arkaria.postgresql.org; Thu, 30 Jan 2014 16:00:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W8u2t-0004hm-Mb for pgsql-performance@arkaria.postgresql.org; Thu, 30 Jan 2014 16:00:51 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8u2s-0004hg-Oh for pgsql-performance@postgresql.org; Thu, 30 Jan 2014 16:00:50 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8u2l-0008IS-PT for pgsql-performance@postgresql.org; Thu, 30 Jan 2014 16:00:50 +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 s0UG0fQd032498; Thu, 30 Jan 2014 11:00:41 -0500 From: Tom Lane To: jugnooken cc: pgsql-performance@postgresql.org Subject: Re: WHERE with ORDER not using the best index In-reply-to: <1391046439075-5789624.post@n5.nabble.com> References: <1391030357379-5789581.post@n5.nabble.com> <9852.1391037355@sss.pgh.pa.us> <1391046439075-5789624.post@n5.nabble.com> Comments: In-reply-to jugnooken message dated "Wed, 29 Jan 2014 17:47:19 -0800" Date: Thu, 30 Jan 2014 11:00:41 -0500 Message-ID: <32497.1391097641@sss.pgh.pa.us> X-Pg-Spam-Score: -0.9 (/) 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 jugnooken writes: > Unfortunately, pg still thinks using > index_social_feed_feed_items_on_social_feed_id is faster although they are > about the same size :(. Any idea? On further reflection, the cost estimate that is weird for this number of rows is not the large one for your preferred index, but the small estimate for the one the planner likes. My guess is that that must be happening because the latter index is nearly perfectly correlated with the table's physical order, whereas yours is more or less random relative to table order. The fact that the former index is actually faster in use means that in your environment, random access into the table is pretty cheap, which means you should consider decreasing random_page_cost. But first it'd be a good idea to confirm that your test case is actually representative of production behavior --- it's very easy to get fooled by all-in-cache measurements, which are not reliable guides unless your database does in fact fit in RAM. 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