Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id E5EA99FB514 for ; Wed, 17 Oct 2007 10:02:20 -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 79310-10 for ; Wed, 17 Oct 2007 10:01:59 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.169]) by postgresql.org (Postfix) with ESMTP id 51A0C9FB489 for ; Wed, 17 Oct 2007 10:02:15 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id j3so244656ugf for ; Wed, 17 Oct 2007 06:02:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; bh=N8K1qpfswrVnRdta8ZxOWS8i5FFOqJGdpx2xz8x4NAE=; b=GArjSOBWnKnaIZRNS4ZBTDLnOxBtD4j+3qXLi1lbhwmDaa6/pD/faRTpsKF/kpfHqiyqJJKcHt3qgvOO4Q2awLb/lCzvB8U5ndVqi34GkIldevVlQF1M9T/Ysjh7Iczcs9e3PIudUgmF+7sV8741qWDrSOadR4ktxK/sIrQL/UI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=KDyB6P5bPY7YqsrIhrf/gRV4uGIJCuRa2C8AwPNYbxfm3pYx+TPFtlieXeb0ZuIMb60Ask4BDavsXEoOuYVQAFfyx5vAtCI5MAIipJxwTrc2/aLX2OS9H+e71vdTGn5iNd2QtdRrdK9cZGyu04k4+m1WiNLgr2Q1d1iyWkn1FKg= Received: by 10.66.252.8 with SMTP id z8mr580994ugh.1192626133454; Wed, 17 Oct 2007 06:02:13 -0700 (PDT) Received: from ?172.24.32.198? ( [62.232.55.118]) by mx.google.com with ESMTPS id g30sm3628353ugd.2007.10.17.06.02.04 (version=SSLv3 cipher=RC4-MD5); Wed, 17 Oct 2007 06:02:06 -0700 (PDT) Message-ID: <4716078A.1030505@enterprisedb.com> Date: Wed, 17 Oct 2007 14:00:58 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.12 (X11/20070730) MIME-Version: 1.0 To: Stefano Dal Pra CC: pgsql-sql@postgresql.org, pgsql-performance@postgresql.org Subject: Re: two queryes in a single tablescan References: <290a5abc0710170530v215cb2b0q4fa000ad43096a28@mail.gmail.com> In-Reply-To: <290a5abc0710170530v215cb2b0q4fa000ad43096a28@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/198 X-Sequence-Number: 27220 Stefano Dal Pra wrote: > suppose you have a large table tab and two (or more) queryes like this: > > SELECT count(*),A FROM tab WHERE C GROUP BY A; > SELECT count(*),B FROM tab WHERE C GROUP BY B; > > is there any way to get both results in a single query, > eventually through stored procedure? > The retrieved [count(*),A] ; [count(*),B)] data couldnt fit > on a single table, of course. > > The main goal would be to get multiple results while scanning the > table[s] once only > thus getting results in a faster way. > > This seems to me quite a common situation but i have no clue whether a neat > solution can be implemented through stored procedure. With a temp table: CREATE TEMPORARY TABLE tmp AS SELECT COUNT(*) as rows, a,b FROM WHERE C GROUP BY a,b; SELECT SUM(rows), a FROM tmp GROUP BY a; SELECT SUM(rows), b FROM tmp GROUP BY b; DROP TABLE tmp; (Using temp tables in plpgsql procedures doesn't quite work until 8.3. But you can use dynamic EXECUTE as a work-around. There used to be a FAQ entry about that, but apparently it's been removed because the problem has been fixed in the upcoming release.) -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com