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 9174811F76F4 for ; Fri, 21 Apr 2006 09:39:05 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.096 X-Spam-Level: X-Spam-Status: No, score=0.096 required=5 tests=[AWL=0.096] Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id sB9frFqZwM1x for ; Fri, 21 Apr 2006 09:38:59 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from amanda.contactbda.com (ipn36372-e65122.cidr.lightship.net [216.204.66.226]) by postgresql.org (Postfix) with ESMTP id 98EBB11F76BE for ; Fri, 21 Apr 2006 09:39:02 -0300 (ADT) Received: from amanda.contactbda.com (amanda.contactbda.com [192.168.1.2]) by amanda.contactbda.com (8.12.11/8.12.11/Debian-3) with ESMTP id k3LCcwtd008361; Fri, 21 Apr 2006 08:38:58 -0400 From: "Jim Buttafuoco" To: Jan Dittmer Cc: pgsql-performance@postgresql.org Reply-To: jim@contactbda.com Subject: Re: Better way to write aggregates? Date: Fri, 21 Apr 2006 08:38:58 -0400 Message-Id: <20060421123759.M11447@contactbda.com> In-Reply-To: <4448D195.4020801@l4x.org> References: <444899B6.6090001@l4x.org> <20060421122617.M81850@contactbda.com> <4448D195.4020801@l4x.org> X-Mailer: Open WebMail 2.41 20040926 X-OriginatingIP: 192.168.1.1 (jim) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Archive-Number: 200604/499 X-Sequence-Number: 18583 I don't think an index will help you with this query. ---------- Original Message ----------- From: Jan Dittmer To: jim@contactbda.com Cc: pgsql-performance@postgresql.org Sent: Fri, 21 Apr 2006 14:35:33 +0200 Subject: Re: [PERFORM] Better way to write aggregates? > Jim Buttafuoco wrote: > > Jan, > > > > I write queries like this > > > > CREATE VIEW parent_childs AS > > SELECT > > c.parent, > > count(c.state) as childtotal, > > sum(case when c.state = 1 then 1 else 0 end) as childstate1, > > sum(case when c.state = 2 then 1 else 0 end) as childstate2, > > sum(case when c.state = 3 then 1 else 0 end) as childstate3 > > FROM child c > > GROUP BY parent; > > It would help if booleans could be casted to integer 1/0 :-) But > performance wise it should be about the same? I think I'll > run some tests later today with real data. > Would an index on NULLIF(state,1) help count(NULLIF(state,1)) ? > Can one build an index on (case when c.state = 3 then 1 else 0 end)? > > Thanks, > > Jan ------- End of Original Message -------