Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YEqat-0001tm-OX for pgsql-sql@arkaria.postgresql.org; Sat, 24 Jan 2015 02:37:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YEqas-0001Wn-1e for pgsql-sql@arkaria.postgresql.org; Sat, 24 Jan 2015 02:37:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YEqaq-0001We-0o for pgsql-sql@postgresql.org; Sat, 24 Jan 2015 02:37:00 +0000 Received: from mail-ob0-x22c.google.com ([2607:f8b0:4003:c01::22c]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YEqak-0003pd-4T for pgsql-sql@postgresql.org; Sat, 24 Jan 2015 02:36:57 +0000 Received: by mail-ob0-f172.google.com with SMTP id wp18so683066obc.3 for ; Fri, 23 Jan 2015 18:36:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=SmtREUgGRNKK+RoJbPJ7eImBu9+GUy0ySp+VBkp1ZuI=; b=i3LIEHnvcZOSPMyf8V1XFqjJK/Qos/URxofdx+jLyEnKvI6HbpwY8ykAy2p/TrecF+ ekgst9GFVWXFeN2z4O3ZVkQgyY+crA8tL4Lk0jSz/uIzTKHH7c5R0K3OF3PZC9FaMJeS 6kSAya4sZewyyFALlGE4cuMa5ZXPqwYe1ggb2ne/kXiPi4+b0JyyDfqEKq9t0NrRrJPT QjlJXoXZB6uNd2+/z20VA+AWkaWcsR1e/tsbJmF3IlDKBeoWk2FKHhEavCh7tbtXgfS4 uDc/ORkBmIqAZBUXm4/CmZ4MxZ1UR7wk9TR57VcSC9MTbkacQkhbcdrPTMhZS5UPia+K Se7Q== X-Received: by 10.183.24.162 with SMTP id ij2mr6538183obd.18.1422067011189; Fri, 23 Jan 2015 18:36:51 -0800 (PST) Received: from [10.0.1.180] ([173.218.123.97]) by mx.google.com with ESMTPSA id mp3sm899573obb.25.2015.01.23.18.36.50 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Jan 2015 18:36:50 -0800 (PST) Message-ID: <54C30541.7050908@gmail.com> Date: Fri, 23 Jan 2015 20:36:49 -0600 From: Jason Aleski User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Better way to compute moving averages? References: <54C2D22B.8050605@gmail.com> <1422060627285-5835295.post@n5.nabble.com> In-Reply-To: <1422060627285-5835295.post@n5.nabble.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 1/23/2015 6:50 PM, David G Johnston wrote: > Jason Aleksi wrote >> I've been asked compute various moving averages of end of day sales by >> store. I can do this for all rows with no problem (same query without >> the WHERE clause towards the end). That query took 10-15 minutes to run >> over approx 3.4 million rows. I'm sure they will want this information >> to be added to the daily end of day reports. I can run the query below >> (excluding the WHERE clause) but it takes almost as long to run one day >> as it does the entire dataset. It looks like when I do the inner >> select, it is still running over the entire dataset. I have added a >> "WHERE eod_ts > CURRENT_TIMESTAMP - INTERVAL '365 days'" (as below) to >> the inner query, which allows the query to run between 1-2 minutes. >> >> Question 1) This seems to work, but was curious if there is a better way. > Possibly though I'm not sure you've given enough info... > > Regardless, are you positive you need "FOLLOWING" instead of "PRECEDING" > frames? > > >> Question 2) Is there a way to specify a date, instead of using current >> date and current_timestamp, as a variable and use that in the query? I >> know I can do that in my Java program >> using variables, but wasn't sure if there was a way to do this with a >> function or stored procedure? > Have you read about functions in the documentation? > > CREATE FUNCTION echo_text(input_var text) > RETURNS text > AS $$ > SELECT input_var; > $$ > LANGUAGE sql > STRICT > ; > > >> It looks like when I do the inner >> select, it is still running over the entire dataset. >> I have added a >> "WHERE eod_ts > CURRENT_TIMESTAMP - INTERVAL '365 days'" (as below) to >> the inner query, which allows the query to run between 1-2 minutes. >> [...] >> FROM end_of_day_data >> WHERE eod_ts > CURRENT_TIMESTAMP - INTERVAL '260 days' >> GROUP BY store_id, eod_ts, eod_sales >> ORDER BY ticker_id, eod_ts >> ) as t1 >> WHERE t1.eod_ts = current_date >> ); > You expected differently? GROUP BY (in particular, not only) prevents any > kind of push-down of quals. > > Try something like (not tested or tried - just having an idea this moment): > > WITH scope_of_source_with_aging AS ( > SELECT eod_ts, > first(eod_ts) OVER () AS ref_eod, > eod_ts - first(eod_ts) OVER (ORDER BY eod_ts) AS eod_age > FROM end_of_day_data WHERE eod_ts >= current_timestamp - interval '206 days' > ) > SELECT ref_eod, > avg(CASE WHEN eod_age BETWEEN 0 AND 7 THEN ... ELSE NULL END) AS > average_7day > [, ...] > FROM scope_of_source_with_aging > GROUP BY ref_eod > ; > > The first, and most important part, is to limit your source data to only the > desired time period. The rest of the above simply tries to limit how much > work needs to be done. Since window function, while great, are fairly slow > if you can minimize their use - in this case by limiting them to simply > calculating and age - it should help performance. > > The concept above should be applicable to your situation generally but you > will need to clarify your needs and then modify the WHERE and window clauses > as appropriate. > > I also don't recall if there is a native "first" function though you can > either write one yourself or possibly add an appropriate range clause that > will accomplish the same goal. > > David J. > > > > > > > -- > View this message in context: http://postgresql.nabble.com/Better-way-to-compute-moving-averages-tp5835291p5835295.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > > David J. Thanks for the info. 1) Yes, I pretty sure I need FOLLOWING because I ordered the eod_ts (timestamp) using DESC. 2) The only functions I've created in the past have been primarily relating to queries that don't return any data. Things such as "purge duplicate rows". Nothing that used input and output; only to execute tasks on the backend. Also, I'll look at the WITH method again. I saw that in an earlier search, but didn't fully understand it and looked for another way. In both cases, thanks for the reassurance and pointing me in a direction to explore. Regards, JA -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql