X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DC5AD475E14 for ; Sun, 11 May 2003 14:53:30 -0400 (EDT) Received: from mx3.absolight.com (mx3.absolight.net [212.43.217.9]) by postgresql.org (Postfix) with ESMTP id 1161D4758F1 for ; Sun, 11 May 2003 14:53:26 -0400 (EDT) Received: by mx3.absolight.com (Postfix, from userid 1007) id 2799D2A15; Sun, 11 May 2003 20:53:26 +0200 (CEST) X-Original-To: pgsql-sql@postgresql.org Received: from sauron.in.mat.cc (sauron.in.mat.cc [212.43.217.122]) (using TLSv1 with cipher EDH-RSA-DES-CBC3-SHA (168/168 bits)) (Client did not present a certificate) by mx3.absolight.com (Postfix) with ESMTP id CAD222A08; Sun, 11 May 2003 20:53:24 +0200 (CEST) Date: Sun, 11 May 2003 20:53:46 +0200 From: Mathieu Arnold To: Chris Linstruth , pgsql-sql@postgresql.org Subject: Re: sub select performance Message-ID: <3465828.1052686426@sauron.in.mat.cc> In-Reply-To: References: X-Mailer: Mulberry/3.0.3 (Win32) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Spam-Status: No, hits=-16.3 required=5.0 tests=BAYES_01,IN_REP_TO,REFERENCES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/209 X-Sequence-Number: 13374 --Le 11/05/2003 09:48 -0700, Chris Linstruth =E9crivait : | | But this can take 15-30 seconds: |=20 | SELECT count(radacctid) AS sessions, | sum(acctsessiontime) AS connecttime | FROM radacct | WHERE radacctid IN | (SELECT DISTINCT ON (acctsessionid) radacctid FROM radacct | WHERE username=3D'cjl' | AND acctstoptime IS NOT NULL | AND date_trunc('month', now())=3Ddate_trunc('month', | acctstoptime)); |=20 | There are probably many different ways to perform this query. My | main problem is trying to overcome the fact that try as I might, | I can't stop the occasional duplicate accounting record from being | inserted so I have to weed them out, hence the "DISTINCT ON | (acctsessionid)". try this : SELECT count(radacctid) AS sessions, sum(acctsessiontime) AS connecttime FROM radacct JOIN (SELECT DISTINCT ON (acctsessionid) radacctid FROM radacct WHERE username=3D'cjl' AND acctstoptime IS NOT NULL AND date_trunc('month', now())=3Ddate_trunc('month', acctstoptime)) AS subselect USING (radacctid); --=20 Mathieu Arnold