pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rosser Schwarz <rosser.schwarz@gmail.com>
To: matt@followers.net
Cc: pgsql-performance@postgresql.org
Subject: Re: Preventing query from hogging server
Date: Thu, 24 Mar 2005 13:24:12 -0600
Message-ID: <37d451f7050324112414d7c9b5@mail.gmail.com> (raw)
In-Reply-To: <42431001.36c6643c.1b4b.ffffd505@mx.gmail.com>
References: <42431001.36c6643c.1b4b.ffffd505@mx.gmail.com>

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



view thread (9+ messages)  latest in thread

Message-ID: <37d451f7050324112414d7c9b5@mail.gmail.com>
Permalink:  ../37d451f7050324112414d7c9b5@mail.gmail.com/
Also on:    postgresql.org/message-id/37d451f7050324112414d7c9b5@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: rosser.schwarz@gmail.com, matt@followers.net
  Subject: Re: Preventing query from hogging server
  In-Reply-To: <37d451f7050324112414d7c9b5@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox