Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 102B49FA21D for ; Fri, 2 Nov 2007 07:44:30 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 55883-09 for ; Fri, 2 Nov 2007 07:44:27 -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 B22219FA220 for ; Fri, 2 Nov 2007 07:44:26 -0300 (ADT) Received: from localhost (localhost.pingpong.net [127.0.0.1]) by melon.pingpong.net (Postfix) with ESMTP id E9FDF509DE; Fri, 2 Nov 2007 11:44:29 +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 60838-03-7; Fri, 2 Nov 2007 11:44:29 +0100 (CET) Received: from [192.168.1.187] (unknown [213.136.40.204]) by melon.pingpong.net (Postfix) with ESMTP id C0175509D7; Fri, 2 Nov 2007 11:44:29 +0100 (CET) Date: Fri, 02 Nov 2007 11:44:24 +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: <03A6E4AA1C32C2173207DBA5@rambutan.pingpong.net> In-Reply-To: <6024.1193931282@sss.pgh.pa.us> References: <4305.1193924619@sss.pgh.pa.us> <5504.1193929617@sss.pgh.pa.us> <16563C7D831BD7B32DFBDDEC@rambutan.pingpong.net> <6024.1193931282@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/33 X-Sequence-Number: 27448 --On torsdag, november 01, 2007 11.34.42 -0400 Tom Lane wrote: > Palle Girgensohn writes: >> --On torsdag, november 01, 2007 11.06.57 -0400 Tom Lane >> wrote: >>> Um, you did re-ANALYZE the table after changing the setting? > >> alter table login alter userid SET statistics 1000; >> vacuum analyze login; > > Hm, that's the approved procedure all right. But the plans didn't > change at all? Not even the estimated number of rows? Estimated number of rows did change from ~400 to ~1900, but the timing was the same. Seems that the problem is that it is using an index on "when". Removing that index (login_when_idx) changes the plan, and makes the query equally fast whether group by is there or not. I may need the index, though, in which one more index, on (userid, "when"), will fix the problem. I'd rather get rid of an index than creating another one. Anyway, I think I have two suggestions for a solution that will work for me. I still think it is strange that the group by clause so radically changes the behaviour and the query time. Cheers, Palle pp=# \d login Table "public.login" Column | Type | Modifiers --------+--------------------------+-------------------- userid | text | kursid | integer | when | timestamp with time zone | mode | text | default 'pm'::text Indexes: "login_kurs_user_idx" btree (kursid, userid) "login_userid_idx" btree (userid) "login_when_idx" btree ("when") Foreign-key constraints: "pp_fk1" FOREIGN KEY (userid) REFERENCES person(userid) ON UPDATE CASCADE ON DELETE CASCADE "pp_fk2" FOREIGN KEY (kursid) REFERENCES course(id) ON UPDATE CASCADE ON DELETE CASCADE