Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 13C346346C1 for ; Thu, 5 Mar 2009 14:33:18 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 32912-02-5 for ; Thu, 5 Mar 2009 14:33:15 -0400 (AST) 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 mail.postgresql.org (Postfix) with ESMTP id 0A1766352D9 for ; Thu, 5 Mar 2009 14:30:52 -0400 (AST) 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 n25IUqoK008054; Thu, 5 Mar 2009 13:30:52 -0500 (EST) To: Jonathan Hseu cc: pgsql-performance@postgresql.org Subject: Re: Index scan plan estimates way off. In-reply-to: <78a8986f0903050756i7b1f6c27teb4c67d4578f11ae@mail.gmail.com> References: <78a8986f0903050756i7b1f6c27teb4c67d4578f11ae@mail.gmail.com> Comments: In-reply-to Jonathan Hseu message dated "Thu, 05 Mar 2009 09:56:48 -0600" Date: Thu, 05 Mar 2009 13:30:51 -0500 Message-ID: <8053.1236277851@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: 200903/53 X-Sequence-Number: 32975 Jonathan Hseu writes: > Sort (cost=11684028.44..11761274.94 rows=30898601 width=40) > Sort Key: "time" > -> Bitmap Heap Scan on ticks (cost=715657.57..6995196.08 rows=30898601 > width=40) > Recheck Cond: (contract_id = 1) > -> Bitmap Index Scan on contract_id_time_idx > (cost=0.00..707932.92 rows=30898601 width=0) > Index Cond: (contract_id = 1) > (6 rows) > This plan doesn't complete in a reasonable amount of time. I end up having > to kill the query after it's been running for over an hour. The bitmap scan should be at least as efficient as the plain indexscan, so I suppose the problem is that the sort is slow. What's the datatype of "time"? Can this machine actually support 256MB+ work_mem, or is that likely to be driving it into swapping? You might learn more from enabling trace_sort and watching the postmaster log entries it generates. On the whole I think the planner isn't making a stupid choice here: sorting a large number of rows usually *is* preferable to making an indexscan over them, unless the table is remarkably close to being in physical order for the index. So it would be worth trying to figure out what the problem with the sort is. regards, tom lane