Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 58B6263292A for ; Mon, 23 Aug 2010 07:58:30 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 61238-07 for ; Mon, 23 Aug 2010 10:58:22 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 54F876327B3 for ; Mon, 23 Aug 2010 07:58:23 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OnUjI-00030g-Dm for pgsql-sql@postgresql.org; Mon, 23 Aug 2010 12:58:16 +0200 Received: from 178-83-162-123.dclient.hispeed.ch ([178.83.162.123]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Aug 2010 12:58:16 +0200 Received: from roederja by 178-83-162-123.dclient.hispeed.ch with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Aug 2010 12:58:16 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: =?UTF-8?B?SmFubiBSw7ZkZXI=?= Subject: Re: Inefficient query plan Date: Mon, 23 Aug 2010 12:58:06 +0200 Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 178-83-162-123.dclient.hispeed.ch User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; de; rv:1.9.2.8) Gecko/20100802 Thunderbird/3.1.2 In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201008/75 X-Sequence-Number: 34515 Please ignore this post. I accidentally double posted here and on the performance mailing list. I think this belongs on the performance list. Sorry for the noise. Jann Am 23.08.10 06:05, schrieb Jann Röder: > I have two tables: > A: ItemID (PK), IsssueID (Indexed) > B: ItemID (FK), IndexNumber : PK(ItemID, IndexNumber) > > Both tables have several million columns, but B has much more than A. > > Now if I run > > SELECT A.ItemID FROM A, B WHERE A.ItemID = B.itemID AND A.issueID = > > > The query takes extremely long (several hours). I ran EXPLAIN and got: > > "Hash Join (cost=516.66..17710110.47 rows=8358225 width=16)" > " Hash Cond: ((b.itemid)::bpchar = a.itemid)" > " -> Seq Scan on b (cost=0.00..15110856.68 rows=670707968 width=16)" > " -> Hash (cost=504.12..504.12 rows=1003 width=16)" > " -> Index Scan using idx_issueid on a (cost=0.00..504.12 > rows=1003 width=16)" > " Index Cond: (issueid = 'A1983PW823'::bpchar)" > > Now we see the problem is the seq scan on B. However there are only a > handful of rows in A that match a specific issueID. So my question is > why doesn't it just check for each of the ItemIDs that have the correct > IssueID in A if there is a matching itemID in B. This should be really > fast because ItemID in B is indexed since it is part of the primary key. > > What is the reason for postgres not doing this, is there a way I can > make it do that? I'm using postgresql 8.4.4 and yes, I did run ANALYZE > on the entire DB. > > Thanks a lot, > Jann > >