Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 4FE0337BA2D for ; Thu, 2 Oct 2008 04:26:57 -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 24703-01 for ; Thu, 2 Oct 2008 04:26:53 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from ptb-relay01.plus.net (ptb-relay01.plus.net [212.159.14.145]) by postgresql.org (Postfix) with ESMTP id 36FC937B9C8 for ; Thu, 2 Oct 2008 04:26:52 -0300 (ADT) Received: from [84.51.143.99] (helo=server3.office.archonet.com) by ptb-relay01.plus.net with esmtp (Exim) id 1KlIaF-0002FH-1f; Thu, 02 Oct 2008 08:26:47 +0100 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id B7328274091; Thu, 2 Oct 2008 08:26:46 +0100 (BST) Message-ID: <48E477B6.7040100@archonet.com> Date: Thu, 02 Oct 2008 08:26:46 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.17 (X11/20080914) MIME-Version: 1.0 To: "H. William Connors II" CC: pgsql-performance@postgresql.org Subject: Re: bizarre query performance question References: <48E3DEE9.5000002@rochgrp.com> In-Reply-To: <48E3DEE9.5000002@rochgrp.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Plusnet-Relay: d419561591eefc8d4bfd15b730c819d6 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: 200810/11 X-Sequence-Number: 31592 H. William Connors II wrote: > fa_assignment has 44184945 records > fa_assignment_detail has 82196027 records > > explain select * from fa_assignment fa JOIN fa_assignment_detail fad ON > (fad.assignment_id = fa.assignment_id) where fa.scenario_id = 0; > > QUERY > PLAN > ------------------------------------------------------------------------------------------------------- > > Hash Join (cost=581289.72..4940729.76 rows=9283104 width=91) Are you really expecting 9 million rows in the result? If so, this is probably a reasonable plan. > Hash Cond: (fad.assignment_id = fa.assignment_id) > -> Seq Scan on fa_assignment_detail fad (cost=0.00..1748663.60 > rows=82151360 width=61) > -> Hash (cost=484697.74..484697.74 rows=4995439 width=30) > -> Bitmap Heap Scan on fa_assignment fa > (cost=93483.75..484697.74 rows=4995439 width=30) > Recheck Cond: (scenario_id = 0) > -> Bitmap Index Scan on fa_assignment_idx2 > (cost=0.00..92234.89 rows=4995439 width=0) > Index Cond: (scenario_id = 0) It's restricting on scenario_id, building a bitmap to identify which disk-blocks will contain one or more matching rows and then scanning those. If those 5 million scenario_id=0 rows are spread over 10% of the blocks then that's a good idea. If it was expecting only a handful of rows with scenario_id=0 then I'd expect it to switch to a "standard" index scan. If your work_mem is small try something like: set work_mem = '50MB'; before running the query - maybe even larger. -- Richard Huxton Archonet Ltd