Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id E5CDE9FB32B for ; Thu, 1 Nov 2007 11:36:59 -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 22813-02-7 for ; Thu, 1 Nov 2007 11:36:32 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from melon.pingpong.net (melon.pingpong.net [195.178.174.161]) by postgresql.org (Postfix) with ESMTP id C42879FB450 for ; Thu, 1 Nov 2007 11:36:31 -0300 (ADT) Received: from localhost (localhost.pingpong.net [127.0.0.1]) by melon.pingpong.net (Postfix) with ESMTP id 1EC06508C5; Thu, 1 Nov 2007 15:36:35 +0100 (CET) Received: from melon.pingpong.net ([127.0.0.1]) by localhost (melon.pingpong.net [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 91667-01-19; Thu, 1 Nov 2007 15:36:34 +0100 (CET) Received: from [192.168.1.187] (unknown [213.136.40.204]) by melon.pingpong.net (Postfix) with ESMTP id EA8CB50899; Thu, 1 Nov 2007 15:36:34 +0100 (CET) Date: Thu, 01 Nov 2007 15:36:29 +0100 From: Palle Girgensohn To: Tom Lane cc: pgsql-performance@postgresql.org Subject: Re: select max(field) from table much faster with a group by clause? Message-ID: In-Reply-To: <4305.1193924619@sss.pgh.pa.us> References: <4305.1193924619@sss.pgh.pa.us> X-Mailer: Mulberry/4.0.8 (Linux/x86) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: by amavisd-new at pingpong.net X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/15 X-Sequence-Number: 27430 --On torsdag, november 01, 2007 09.43.39 -0400 Tom Lane wrote: > Palle Girgensohn writes: >> When running >> select max("when") from login where userid='userid' >> it takes a second or two, but when adding "group by userid" the planner >> decides on using another plan, and it gets *much* faster. See example >> below. > > It's only faster for cases where there are few or no rows for the > particular userid ... > >> Number of tuples per user varies from zero to a couple of thousands. > > The planner is using an intermediate estimate of 406 rows. You might be > well advised to increase the statistics target for login.userid --- with > luck that would help it to choose the right plan type for both common > and uncommon userids. Unfortunately, altering statistics doesn't help. I see no difference when altering the value from 10 (default) to 100, 1000 or 100000. :-( Are there any other things I can modify? OH, btw, maybe something in the postgresql.conf sucks? max_connections = 100 shared_buffers = 30000 # min 16 or max_connections*2, 8KB each temp_buffers = 2500 # min 100, 8KB each max_prepared_transactions = 100 # can be 0 or more work_mem = 16384 # min 64, size in KB maintenance_work_mem = 16384 # min 1024, size in KB max_stack_depth = 32768 # min 100, size in KB max_fsm_pages = 500000 max_fsm_relations = 20000 max_files_per_process = 2000 fsync = off checkpoint_segments = 50 # in logfile segments, min 1, 16MB each effective_cache_size = 10000 # typically 8KB each random_page_cost = 1.8 geqo = on geqo_threshold = 10 from_collapse_limit = 8 join_collapse_limit = 8 # 1 disables collapsing of explicit