X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 546BD9DC82E for ; Mon, 19 Dec 2005 16:47:45 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44043-06-2 for ; Mon, 19 Dec 2005 16:47:43 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from stark.xeocode.com (stark.xeocode.com [216.58.44.227]) by postgresql.org (Postfix) with ESMTP id 31D469DC9E9 for ; Mon, 19 Dec 2005 16:47:39 -0400 (AST) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1EoRux-0000KR-00; Mon, 19 Dec 2005 15:47:35 -0500 To: "Cristian Prieto" Cc: Subject: Re: Any way to optimize GROUP BY queries? References: <00d701c604c1$e6945d80$6500a8c0@gt.ClickDiario.local> In-Reply-To: <00d701c604c1$e6945d80$6500a8c0@gt.ClickDiario.local> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 19 Dec 2005 15:47:35 -0500 Message-ID: <87wti0ak60.fsf@stark.xeocode.com> Lines: 25 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.08 required=5 tests=[AWL=0.080] X-Spam-Score: 0.08 X-Spam-Level: X-Archive-Number: 200512/464 X-Sequence-Number: 16285 "Cristian Prieto" writes: > SELECT adv, pub, web, country, date_trunc('hour', tiempo), sum(num) > FROM mytmp GROUP BY adv, pub, web, country, date_trunc('hour', tiempo) > > I've tried to create index in different columns but it seems that the group > by clause doesn't use the index in any way. If you had an index on < adv,pub,web,country,date_trunc('hour',tiemp) > then it would be capable of using the index however it would choose not to unless you forced it to. Using the index would be slower. > Is around there any stuff to accelerate the group by kind of clauses? Increase your work_mem (or sort_mem in older postgres versions), you can do this for the server as a whole or just for this one session and set it back after this one query. You can increase it up until it starts causing swapping at which point it would be counter productive. If increasing work_mem doesn't allow a hash aggregate or at least an in-memory sort to handle it then putting the pgsql_tmp directory on a separate spindle might help if you have any available. -- greg