Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id EF762650115 for ; Thu, 7 Aug 2008 20:48:18 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 01173-06 for ; Thu, 7 Aug 2008 20:48:11 -0300 (ADT) 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 postgresql.org (Postfix) with ESMTP id 1EE6964FFB8 for ; Thu, 7 Aug 2008 20:48:15 -0300 (ADT) 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 m77NmBh3029509; Thu, 7 Aug 2008 19:48:11 -0400 (EDT) To: "Joshua Shanks" cc: "Craig Ringer" , pgsql-performance@postgresql.org Subject: Re: query planner not using the correct index In-reply-to: <84f0acdb0808071501n15b0182ga7eca7b1be37d705@mail.gmail.com> References: <84f0acdb0808061435q5ebb607cjc311f1eb4da841a@mail.gmail.com> <489A623A.4090400@postnewspapers.com.au> <84f0acdb0808062014p29cf7482ied332225a0a92c63@mail.gmail.com> <489A6AF5.50908@postnewspapers.com.au> <84f0acdb0808070811l43a3da9eq240d272f8ddd105c@mail.gmail.com> <27696.1218143560@sss.pgh.pa.us> <84f0acdb0808071501n15b0182ga7eca7b1be37d705@mail.gmail.com> Comments: In-reply-to "Joshua Shanks" message dated "Thu, 07 Aug 2008 17:01:53 -0500" Date: Thu, 07 Aug 2008 19:48:11 -0400 Message-ID: <29507.1218152891@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200808/62 X-Sequence-Number: 30943 "Joshua Shanks" writes: > SELECT null_frac, n_distinct, most_common_vals, most_common_freqs FROM > pg_stats WHERE tablename = 'bars' AND attname='bars_id'; > null_frac | n_distinct | most_common_vals | most_common_freqs > -----------+------------+----------------------+--------------------------- > 0 | 14 | {145823,47063,24895} | {0.484667,0.257333,0.242} > Those 3 values in reality and in the stats account for 98% of the > rows. actual distinct values are around 350 So you need to increase the stats target for this column. With those numbers the planner is going to assume that any value that's not one of the big three appears about (1 - (0.484667+0.257333+0.242)) / 11 of the time, or several hundred times in 300K rows. If n_distinct were up around 350 it would be estimating just a dozen or so occurrences, which should push the join plan into the shape you want. It's likely that it won't bother to include any more entries in most_common_vals no matter how much you raise the target; but a larger sample should definitely give it a better clue about n_distinct. regards, tom lane