Skip site navigation (1) Skip section navigation (2)

Re: Preventing query from hogging server

From: Rosser Schwarz <rosser(dot)schwarz(at)gmail(dot)com>
To: matt(at)followers(dot)net
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Preventing query from hogging server
Date: 2005-03-24 19:24:12
Message-ID: 37d451f7050324112414d7c9b5@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-performance
while you weren't looking, Matthew Nuzum wrote:

> select accountid, min(atime) as atime, sessionid from usage_access
> group by accountid,sessionid;

Try something along the lines of:

select ua.accountid
     , (select atime
          from usage_access
         where sessionid = ua.sessionid
           and accountid = ua.accountid
         order by atime asc
         limit 1
       ) as atime
     , ua.sessionid
  from usage_access ua
 group by accountid
     , sessionid

min() and max() currently do table scans, which, on large tables, or
even moderately sized tables with large numbers of accounts/sessions,
can add up.  You'll need to replace asc with desc in the subquery for
the max() version.

This form cheats a bit and uses the index to find the highest and
lowest values, provided you've created the appropriate indices.

This is, IIRC, in the FAQ.

/rls

-- 
:wq

In response to

pgsql-performance by date

Next:From: Tom LaneDate: 2005-03-24 19:32:59
Subject: Re: pg_autovacuum not having enough suction ?
Previous:From: Tom LaneDate: 2005-03-24 19:22:22
Subject: Re: BUG #1552: massive performance hit between 7.4 and 8.0.1

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group