Re: Query performance - Mailing list pgsql-performance

From Bill
Subject Re: Query performance
Date
Msg-id 200406301346.i5UDktfL013134@math.uchicago.edu
Whole thread Raw
In response to Re: Query performance  (Bruno Wolff III <bruno@wolff.to>)
Responses Re: Query performance
List pgsql-performance
Thanks this query works for what I want.  So here is an output of the
explain analyze:
                                                                     QUERY
PLAN
----------------------------------------------------------------------------
------------------------------------------------------------------------
 Limit  (cost=2421582.59..2421582.65 rows=25 width=29) (actual
time=1985800.32..1985800.44 rows=25 loops=1)
   ->  Sort  (cost=2421582.59..2424251.12 rows=1067414 width=29) (actual
time=1985800.31..1985800.35 rows=26 loops=1)
         Sort Key: avg(((open - "close") / (open + 1::numeric)))
         ->  Aggregate  (cost=2200163.04..2280219.09 rows=1067414 width=29)
(actual time=910291.94..1984972.93 rows=22362 loops=1)
               ->  Group  (cost=2200163.04..2253533.74 rows=10674140
width=29) (actual time=910085.96..1105064.28 rows=10674140 loops=1)
                     ->  Sort  (cost=2200163.04..2226848.39 rows=10674140
width=29) (actual time=910085.93..988909.94 rows=10674140 loops=1)
                           Sort Key: symbol
                           ->  Seq Scan on oclh  (cost=0.00..228404.40
rows=10674140 width=29) (actual time=20.00..137720.61 rows=10674140 loops=1)
 Total runtime: 1986748.44 msec
(9 rows)

Can I get any better performance?

Thanks.

-----Original Message-----
From: Bruno Wolff III [mailto:bruno@wolff.to]
Sent: Tuesday, June 29, 2004 2:52 PM
To: Bill
Cc: pgsql-performance@postgresql.org
Subject: Re: [PERFORM] Query performance

On Tue, Jun 29, 2004 at 12:33:51 -0500,
  Bill <bill@math.uchicago.edu> wrote:
> Ok, thanks.  So let me explain the query number 2 as this is the more
> difficult to write.  So I have a list of stocks, this table contains the
> price of all of the stocks at the open and close date.  Ok, now we have a
> ratio from query (1) that returns at least a very rough index of the daily
> performance of a given stock, with each ratio representing the stock's
> performance in one day.  Now we need to average this with the same stock's
> ratio every day, to get a total average for each stock contained in the
> database.  Now I would simply like to find a ratio like this that
represents
> the average of every stock in the table and simply find the greatest
ratio.
> Sorry about the lousy explanation before, is this a bit better?

You can do something like:

SELECT symbol, avg((open-close)/open) GROUP BY symbol
  ORDER BY avg((open-close)/open) DESC LIMIT 1;

If you aren't interested in the variance of the daily change, it seems like
you would be best off using the opening price for the first day you have
recorded for the stock and the closing price on the last day and looking
at the relative change.


pgsql-performance by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: postgres 7.4 at 100%
Next
From: Rod Taylor
Date:
Subject: Re: Query performance