Re: sub select performance

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Chris Linstruth <cjl(at)QNET(dot)COM>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: sub select performance
Date: 2003-05-11 17:51:52
Message-ID: 20030511175152.GC710@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sun, May 11, 2003 at 09:48:05 -0700,
Chris Linstruth <cjl(at)QNET(dot)COM> wrote:
> I'm trying to use a subselect and am not sure why performance suffers.
>
> 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)".

IN is slow. If you tried the development version it would probably be
a lot faster. For 7.3 and below, try rewriting the query to use a join
or a where clause.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mathieu Arnold 2003-05-11 18:53:46 Re: sub select performance
Previous Message Peter Eisentraut 2003-05-11 16:54:50 Re: Using psql to insert character codes