X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 930D6D1B184 for ; Wed, 30 Jun 2004 10:46:57 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 71125-05 for ; Wed, 30 Jun 2004 13:46:56 +0000 (GMT) Received: from math.uchicago.edu (math.uchicago.edu [128.135.72.38]) by svr1.postgresql.org (Postfix) with ESMTP id 7611FD1B172 for ; Wed, 30 Jun 2004 10:46:51 -0300 (ADT) Received: from billnotebook (wireless-197-223.uchicago.edu [128.135.197.223]) by math.uchicago.edu (8.12.10/8.12.10) with ESMTP id i5UDktfL013134 for ; Wed, 30 Jun 2004 08:46:55 -0500 Message-Id: <200406301346.i5UDktfL013134@math.uchicago.edu> From: "Bill" To: Subject: Re: Query performance Date: Wed, 30 Jun 2004 08:47:03 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 In-Reply-To: <20040629195130.GA1513@wolff.to> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 Thread-Index: AcReEY+bwr8Aj6a5SVG7hH5fF/GZ0QAlvmYg X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/322 X-Sequence-Number: 7380 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 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.