Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 2B0EE9FBC3D for ; Fri, 12 Oct 2007 19:04:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 59985-01-2 for ; Fri, 12 Oct 2007 19:03:45 -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 B8C399FC127 for ; Fri, 12 Oct 2007 19:03:48 -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 l9CM3k9x005755; Fri, 12 Oct 2007 18:03:46 -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: References: Comments: In-reply-to henk de wit message dated "Fri, 12 Oct 2007 22:41:56 +0200" Date: Fri, 12 Oct 2007 18:03:46 -0400 Message-ID: <5754.1192226626@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/158 X-Sequence-Number: 27180 henk de wit writes: > The plan looks like this: > "Result (cost=3D0.37..0.38 rows=3D1 width=3D0) (actual time=3D184231.636..= > 184231.638 rows=3D1 loops=3D1)" > " InitPlan" > " -> Limit (cost=3D0.00..0.37 rows=3D1 width=3D8) (actual time=3D18423= > 1.620..184231.622 rows=3D1 loops=3D1)" > " -> Index Scan Backward using trans_payment_id_index on transact= > ions (cost=3D0.00..19144690.58 rows=3D51122691 width=3D8) (actual time=3D1= > 84231.613..184231.613 rows=3D1 loops=3D1)" > " Filter: (payment_id IS NOT NULL)" > "Total runtime: 184231.755 ms" 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 :-(. In 8.3 it'll be possible to declare the index as NULLS FIRST, which moves the performance problem from the max end to the min end ... > select min(time) from transactions where payment_id =3D 67 > There are indexes on both the time (a timestamp with time zone) and payment= > _id (a bigint) columns. Creating indexes at random with no thought about how the system could use them is not a recipe for speeding up your queries. What you'd need to make this query fast is a double-column index on (payment_id, time) so that a forward scan on the items with payment_id = 67 would immediately find the minimum time entry. Neither of the single-column indexes offers any way to find the desired entry without scanning over lots of unrelated entries. regards, tom lane