Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 9090D9F93E2 for ; Tue, 5 Dec 2006 23:02:12 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 49373-06 for ; Tue, 5 Dec 2006 23:01:48 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from lynet.oppetid.no (oppetid.no [80.65.51.2]) by postgresql.org (Postfix) with ESMTP id DE4439F9357 for ; Tue, 5 Dec 2006 23:01:59 -0400 (AST) Received: by lynet.oppetid.no (Postfix, from userid 1019) id AA69911AFEB; Wed, 6 Dec 2006 04:01:56 +0100 (CET) Date: Wed, 6 Dec 2006 04:01:56 +0100 From: Tobias Brox To: pgsql-performance@postgresql.org Subject: max/min and index usage Message-ID: <20061206030156.GA29255@oppetid.no> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Organization: Group Nordicbet User-Agent: Mutt/1.5.9i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/100 X-Sequence-Number: 22156 We're using 8.1 - I thought such a construct was safe in pg 8.1: select max(indexed_value) from huge_table; while earlier we had to use: select indexed_value from huge_table order by indexed_value desc limit 1; seems like I was wrong: mydb=> explain analyze select indexed_value1 from mytable order by indexed_value1 desc limit 1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..1.96 rows=1 width=4) (actual time=0.115..0.117 rows=1 loops=1) -> Index Scan Backward using index1 on mytable (cost=0.00..23890756.52 rows=12164924 width=4) (actual time=0.111..0.111 rows=1 loops=1) Total runtime: 0.162 ms (3 rows) mydb=> explain analyze select indexed_value2 from mytable order by indexed_value2 desc limit 1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..0.04 rows=1 width=4) (actual time=0.128..0.130 rows=1 loops=1) -> Index Scan Backward using index2 on mytable (cost=0.00..428231.16 rows=12164924 width=4) (actual time=0.124..0.124 rows=1 loops=1) Total runtime: 0.160 ms (3 rows) mydb=> explain analyze select max(indexed_value2) from mytable; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Result (cost=0.04..0.05 rows=1 width=0) (actual time=11652.138..11652.139 rows=1 loops=1) InitPlan -> Limit (cost=0.00..0.04 rows=1 width=4) (actual time=11652.122..11652.124 rows=1 loops=1) -> Index Scan Backward using index2 on mytable (cost=0.00..428231.16 rows=12164924 width=4) (actual time=11652.117..11652.117 rows=1 loops=1) Filter: (indexed_value2 IS NOT NULL) Total runtime: 11652.200 ms (6 rows) mydb=> explain analyze select max(indexed_value1) from mytable; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Result (cost=1.96..1.97 rows=1 width=0) (actual time=713.780..713.781 rows=1 loops=1) InitPlan -> Limit (cost=0.00..1.96 rows=1 width=4) (actual time=713.767..713.770 rows=1 loops=1) -> Index Scan Backward using index1 on mytable (cost=0.00..23890756.52 rows=12164924 width=4) (actual time=713.764..713.764 rows=1 loops=1) Filter: (indexed_value1 IS NOT NULL) Total runtime: 713.861 ms (6 rows)