pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
ORDER BY and LIMIT with SubSelects
2+ messages / 2 participants
[nested] [flat]

* ORDER BY and LIMIT with SubSelects
@ 2004-01-21 17:18  Ron St-Pierre <rstpierre@syscor.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Ron St-Pierre @ 2004-01-21 17:18 UTC (permalink / raw)
  To: pgsql-performance

I need to get 200 sets of the most recent data from a table for further 
processing, ordered by payDate. My
current solution is to use subselects to:
1 - get a list of unique data
2 - get the 200 most recent records (first 200 rows, sorted descending)
3 - sort them in ascending order

SELECT SSS.* FROM
 (SELECT SS.* FROM
   (SELECT DISTINCT ON (nonUniqField)
    first, second, third, cost, payDate, nonUniqField
    FROM histdata
    WHERE userID = 19048 AND cost IS NOT NULL
   )
  SS  
  ORDER BY SS.payDate DESC LIMIT 200
) SSS
ORDER BY payDate;

My question is in regards to steps 2 and 3 above. Is there some way that 
I can combine both steps into one to save some time?

PostgreSQL 7.4beta2 on i686-pc-linux-gnu, compiled by GCC 2.95.4

Thanks
Ron





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: ORDER BY and LIMIT with SubSelects
@ 2004-01-21 18:36  Bruno Wolff III <bruno@wolff.to>
  parent: Ron St-Pierre <rstpierre@syscor.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Bruno Wolff III @ 2004-01-21 18:36 UTC (permalink / raw)
  To: Ron St-Pierre <rstpierre@syscor.com>; +Cc: pgsql-performance

On Wed, Jan 21, 2004 at 09:18:18 -0800,
  Ron St-Pierre <rstpierre@syscor.com> wrote:
> 
> My question is in regards to steps 2 and 3 above. Is there some way that 
> I can combine both steps into one to save some time?
 
TIP 4: Don't 'kill -9' the postmaster
SELECT SS.* FROM
(SELECT DISTINCT ON (nonUniqField)
 first, second, third, cost, payDate, nonUniqField
 FROM histdata
 WHERE userID = 19048 AND cost IS NOT NULL 
 ORDER BY nonUniqField, payDate DESC LIMIT 200
)
SS  
ORDER BY payDate;



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-01-21 18:36 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-01-21 17:18 ORDER BY and LIMIT with SubSelects Ron St-Pierre <rstpierre@syscor.com>
2004-01-21 18:36 ` Bruno Wolff III <bruno@wolff.to>

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