Re: sub select performance

From: Mathieu Arnold <mat(at)mat(dot)cc>
To: Chris Linstruth <cjl(at)QNET(dot)COM>, pgsql-sql(at)postgresql(dot)org
Subject: Re: sub select performance
Date: 2003-05-11 18:53:46
Message-ID: 3465828.1052686426@sauron.in.mat.cc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

--Le 11/05/2003 09:48 -0700, Chris Linstruth écrivait :

|
| But this can take 15-30 seconds:
|
| SELECT count(radacctid) AS sessions,
| sum(acctsessiontime) AS connecttime
| FROM radacct
| WHERE radacctid IN
| (SELECT DISTINCT ON (acctsessionid) radacctid FROM radacct
| WHERE username='cjl'
| AND acctstoptime IS NOT NULL
| AND date_trunc('month', now())=date_trunc('month',
| acctstoptime));
|
| 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='cjl'
AND acctstoptime IS NOT NULL
AND date_trunc('month', now())=date_trunc('month',
acctstoptime)) AS subselect USING (radacctid);

--
Mathieu Arnold

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David W Noon 2003-05-11 21:10:20 Re: please help construct an SQL query
Previous Message Bruno Wolff III 2003-05-11 17:51:52 Re: sub select performance