X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 3791BD1B8CC for ; Fri, 29 Aug 2003 00:11:05 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 26754-05 for ; Thu, 28 Aug 2003 21:10:36 -0300 (ADT) Received: from mail.speakeasy.net (mail7.speakeasy.net [216.254.0.207]) by svr1.postgresql.org (Postfix) with ESMTP id 04D82D1B8BC for ; Thu, 28 Aug 2003 21:10:31 -0300 (ADT) Received: (qmail 13033 invoked by uid 64014); 29 Aug 2003 00:10:34 -0000 Received: from kgeis@speakeasy.org by mail7.speakeasy.net with AmikaGuardian-Server-2.0.0 (Processed in 0.191544 secs); 29 Aug 2003 00:10:34 -0000 X-AmikaGuardian-Id: mail7.speakeasy.net106211583423613026 X-AmikaGuardian-Category: AN:Non-Junk Content : -2.0 X-AmikaGuardian-Category: AN:Non-Junk Clues : -2.0 X-AmikaGuardian-Category: AN:Picky Clues : 0.1 X-AmikaGuardian-Category: AN:Spam Structure : 0.1 X-AmikaGuardian-Category: AN:Junk Mail : -1.9 X-AmikaGuardian-Category: AN:Pastime : 0.05 X-AmikaGuardian-Category: AN:Joke : 0.05 X-AmikaGuardian-Category: AN:Obvious Clues : 0.1 X-AmikaGuardian-Category: AN:Spam : 0.1 X-AmikaGuardian-Category: AN:Spam Indicators : 0.1 X-AmikaGuardian-Action: Do Nothing() Received: from unknown (HELO speakeasy.org) (kgeis@[67.113.18.226]) (envelope-sender ) by mail7.speakeasy.net (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 29 Aug 2003 00:10:33 -0000 Message-ID: <3F4E99F7.7080604@speakeasy.org> Date: Thu, 28 Aug 2003 17:10:31 -0700 From: Ken Geis User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: bad estimates / non-scanning aggregates Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/450 X-Sequence-Number: 3266 I'm surprised at the effort pgsql requires to run one of my queries. I don't know how to tune this query. Column | Type | Modifiers ------------+--------------+----------- the_id | integer | not null the_date | date | not null num1 | numeric(9,4) | num2 | numeric(9,4) | num3 | numeric(9,4) | num4 | numeric(9,4) | int1 | integer | Indexes: "the_table_pkey" primary key, btree (the_id, the_date) --------------------------------------- The query I want to run is select stock_id, min(price_date) from day_ends group by stock_id; --------------------------------------- Here's the plan that I get. GroupAggregate (cost=3711244.30..3838308.31 rows=6732 width=8) -> Sort (cost=3711244.30..3753593.36 rows=16939624 width=8) Sort Key: stock_id -> Seq Scan on day_ends (cost=0.00..361892.24 rows=16939624 width=8) If I set enable_seqscan = false, the plan changes to GroupAggregate (cost=0.00..67716299.91 rows=6732 width=8) -> Index Scan using day_ends_pkey on day_ends (cost=0.00..67631584.96 rows=16939624 width=8) --------------------------------------- Now... the first plan uses up tons of temporary space for sorting. The second one just runs and runs and runs. I've tried setting the statistics to 1000 with little effect. So the query can get everything it needs from the index, and a full scan of the index should be faster (the index file is less than half the size of the data file.) So why does the optimizer estimate so high? Also, to get the MIN for a given group, not all values of the index need to be seen. Must pgsql do a full scan because it treats all aggregates in the same way? Are MIN and MAX used often enough to justify special treatment, and could that be cleanly implemented? Perhaps the aggregate function can request the data in a certain order, be told that it is being passed data in a certain order, and return before seeing the entire set of data. Food for thought... Thanks, Ken Geis