X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id BF0EA11F74C9 for ; Fri, 21 Apr 2006 09:35:45 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: -2.549 X-Spam-Level: X-Spam-Status: No, score=-2.549 required=5 tests=[AWL=0.050, BAYES_00=-2.599] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id xgt8qCvkuj9Z for ; Fri, 21 Apr 2006 09:35:39 -0300 (ADT) X-Greylist: delayed 03:58:40.75686 by SQLgrey- Received: from rzcomm22.rz.tu-bs.de (rzcomm22.rz.tu-bs.de [134.169.9.68]) by postgresql.org (Postfix) with ESMTP id 9B2AD11F74AF for ; Fri, 21 Apr 2006 09:35:36 -0300 (ADT) Received: from [134.169.64.209] (imt81.imt.ing.tu-bs.de [134.169.64.209]) by rzcomm22.rz.tu-bs.de (8.13.4/8.13.4) with ESMTP id k3LCZY9Z005768; Fri, 21 Apr 2006 14:35:35 +0200 (envelope-from jdi@l4x.org) Message-ID: <4448D195.4020801@l4x.org> Date: Fri, 21 Apr 2006 14:35:33 +0200 From: Jan Dittmer User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: jim@contactbda.com CC: pgsql-performance@postgresql.org Subject: Re: Better way to write aggregates? References: <444899B6.6090001@l4x.org> <20060421122617.M81850@contactbda.com> In-Reply-To: <20060421122617.M81850@contactbda.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200604/498 X-Sequence-Number: 18582 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