Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 642D9634480 for ; Fri, 11 Jun 2010 08:18:59 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 99679-06 for ; Fri, 11 Jun 2010 11:18:52 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id E247D6341F9 for ; Fri, 11 Jun 2010 08:18:51 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1ON2G9-0005u2-HB for pgsql-performance@postgresql.org; Fri, 11 Jun 2010 13:18:49 +0200 Received: from d220035.adsl.hansenet.de ([80.171.220.35]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 11 Jun 2010 13:18:49 +0200 Received: from tim by d220035.adsl.hansenet.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 11 Jun 2010 13:18:49 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-performance@postgresql.org To: pgsql-performance@postgresql.org connect(): No such file or directory From: Tim Landscheidt Subject: Re: Analysis Function Date: Fri, 11 Jun 2010 11:18:37 +0000 Organization: Lines: 35 Message-ID: References: <4C119B3E.3090608@squeakycode.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: d220035.adsl.hansenet.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:AwEw8TTqJyDUWICuPh7jm4N9DJQ= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.011 tagged_above=-10 required=5 tests=BAYES_40=-0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201006/205 X-Sequence-Number: 39061 David Jarvis wrote: >> Have you tested DATE_TRUNC()? > Not really; it returns a full timestamp and I would still have to > concatenate strings. My goal is to speed up the following code (where > *p_*parameters are user inputs): > * date(extract(YEAR FROM m.taken)||''-'||p_month1||'-'||p_day1||''') > d1, > date(extract(YEAR FROM m.taken)||''-'||p_month2||'-'||p_day2||''') > d2* > Using DATE_TRUNC() won't help here, as far as I can tell. Removing the > concatenation will halve the query's time. Such as: > dateserial( m.taken, p_month1, p_day1 ) d1, > dateserial( m.taken, p_month2, p_day2 ) d2 > My testing so far has shown a modest improvement by using a C function (to > avoid concatenation). You could use: | (DATE_TRUNC('year', m.taken) + p_month1 * '1 month'::INTERVAL + p_day1 * '1 day'::INTERVAL)::DATE but whether that is faster or slower I don't know. But I don't see why this query needs to be fast in the first place. It seems to be interactive, and therefore I wouldn't invest too much time to have the user wait not 4.4, but 2.2 seconds. You could also do the concatenation in the ap- plication if that is faster than PostgreSQL's date arithme- tics. Tim