Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 197CD9F9DB4 for ; Fri, 12 Oct 2007 19:17:50 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 56625-05 for ; Fri, 12 Oct 2007 19:17:38 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 10D5F9F9B5C for ; Fri, 12 Oct 2007 19:17:43 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l9CMHf7j005938; Fri, 12 Oct 2007 18:17:41 -0400 (EDT) To: henk de wit cc: "pgsql-performance@postgresql.org" Subject: Re: How to speed up min/max(id) in 50M rows table? In-reply-to: <5754.1192226626@sss.pgh.pa.us> References: <5754.1192226626@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 12 Oct 2007 18:03:46 -0400" Date: Fri, 12 Oct 2007 18:17:41 -0400 Message-ID: <5937.1192227461@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/161 X-Sequence-Number: 27183 I wrote: > The only way I can see for that to be so slow is if you have a very > large number of rows where payment_id is null --- is that the case? > There's not a lot you could do about that in existing releases :-(. Actually, there is a possibility if you are willing to change the query: make a partial index that excludes nulls. Toy example: regression=# create table fooey(f1 int); CREATE TABLE regression=# create index fooeyi on fooey(f1) where f1 is not null; CREATE INDEX regression=# explain select max(f1) from fooey; QUERY PLAN --------------------------------------------------------------- Aggregate (cost=36.75..36.76 rows=1 width=4) -> Seq Scan on fooey (cost=0.00..31.40 rows=2140 width=4) (2 rows) regression=# explain select max(f1) from fooey where f1 is not null; QUERY PLAN ----------------------------------------------------------------------------------------------- Result (cost=0.03..0.04 rows=1 width=0) InitPlan -> Limit (cost=0.00..0.03 rows=1 width=4) -> Index Scan Backward using fooeyi on fooey (cost=0.00..65.55 rows=2129 width=4) Filter: (f1 IS NOT NULL) (5 rows) Probably the planner ought to be smart enough to figure this out without the explicit WHERE in the query, but right now it isn't. regards, tom lane