Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 61C202E0037 for ; Wed, 18 Jun 2008 22:43:45 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 22617-06 for ; Wed, 18 Jun 2008 22:43:37 -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 developer.postgresql.org (Postfix) with ESMTP id A02D72E0030 for ; Wed, 18 Jun 2008 22:43:42 -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 m5J1hY4V010924; Wed, 18 Jun 2008 21:43:34 -0400 (EDT) To: Daniele Varrazzo cc: pgsql-performance@postgresql.org Subject: Re: An "obvious" index not being used In-reply-to: <4859B13F.3070103@develer.com> References: <4859B13F.3070103@develer.com> Comments: In-reply-to Daniele Varrazzo message dated "Thu, 19 Jun 2008 02:07:11 +0100" Date: Wed, 18 Jun 2008 21:43:34 -0400 Message-ID: <10923.1213839814@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/95 X-Sequence-Number: 30445 Daniele Varrazzo writes: > There is an index in the field "foo.account_id" but is not used. The resulting > query plan is: > Aggregate (cost=300940.70..300940.71 rows=1 width=0) (actual > time=13412.088..13412.089 rows=1 loops=1) > -> Hash IN Join (cost=11.97..299858.32 rows=432953 width=0) (actual > time=0.678..13307.074 rows=92790 loops=1) > Hash Cond: (foo.account_id = accounts.id) > -> Seq Scan on foo (cost=0.00..275591.14 rows=5313514 width=4) > (actual time=0.014..7163.538 rows=5313514 loops=1) Well, if the estimate of 432953 rows selected were correct, it'd be right not to use the index. Fetching one row in ten is not a chore for an indexscan. (I'm not sure it'd prefer an indexscan even with an accurate 92K-row estimate, but at least you'd be in the realm where tweaking random_page_cost would make a difference.) I'm not sure why that estimate is so bad, given that you said you increased the stats target on the table. Is there anything particularly skewed about the distribution of the account IDs? regards, tom lane