Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YXtS3-0001NP-O3 for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 15:30:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YXtS3-0005Ek-0n for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 15:30:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YXtS1-0005CN-Rv for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 15:30:38 +0000 Received: from mail-we0-f175.google.com ([74.125.82.175]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YXtRy-0001VY-JT for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 15:30:36 +0000 Received: by wegp1 with SMTP id p1so10547210weg.1 for ; Tue, 17 Mar 2015 08:30:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:organization:user-agent :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=eKuZ+IuoIadLSK0ULqySWWed8JtZkyYf/EgwzXPcAOQ=; b=fGCPh1Q+YMfx3+U7Loqsfgs+SfkME2lVGFL6Vn+3fL+g3nPevz0G4pc4Zw43inju6u fFZTkdJmmyhRkmgpThu+8EqRPhE1zT6kgQv4QzHrCbBVQFA37qFs0b63Ys21BSQEYYZr UelST/Bi+RvnnRnliU3SizRsLINOxEYwIcBLrgrAOUYcKXoel6Tc9mxVmvC911ajEjOn Re6pQ9tB4tUHADza7ZlLORPpNsa3cZ38Vty75t5HOwtwhXh/7rHwqGCb1kCP5401F/Yj rGVIDfSFeoWuWoI7jK/TUdBP1cfDkAT/JDtSlmMVhPYkPYAKuydSlJMTzVm/7jEdB9nZ uUkA== X-Gm-Message-State: ALoCoQkRphmmZQ7esQkzv3GLE9YtKOBJ88VtoMh1zWu27cnBdiNFD8RSwNHJ0Gs3T/t61Fktl/QTaF0sd0Q120uo1MWTTLns2GhlFKifpYVPLhDj0pKGzDTrpwAtPKUZ2Qy9C9qs2KJ0kg1t0RwPAhu8zr3+Un5A3K0WtzxVctakpLlmKjA/2jdidlpiGl5P1BYdPZyMJKvN X-Received: by 10.180.35.97 with SMTP id g1mr55446328wij.17.1426606232927; Tue, 17 Mar 2015 08:30:32 -0700 (PDT) Received: from [192.168.1.163] (ip-78-45-139-138.net.upcbroadband.cz. [78.45.139.138]) by mx.google.com with ESMTPSA id nh17sm3161545wic.5.2015.03.17.08.30.29 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Mar 2015 08:30:31 -0700 (PDT) Message-ID: <55084890.6000907@2ndquadrant.com> Date: Tue, 17 Mar 2015 16:30:24 +0100 From: Tomas Vondra Organization: 2ndQuadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Performance issues References: <84a6fe4be04c3ef0a714851977860ac8@mail.gmail.com> <55034A48.4040409@2ndquadrant.com> <849e75d9463938372dd4c516f5c5d89c@mail.gmail.com> <55037CEA.5050906@2ndquadrant.com> <55073272.20209@BlueTreble.com> <55074424.9050506@2ndquadrant.com> <55076FFB.9090105@BlueTreble.com> c5231342781287ff8cbc9ac1f20ff036@mail.gmail.com <61a7 a27428e588411e8ac1817043205c@mail.gmail.com> <550813CD.7040605@2ndqu adrant.com> d6a86358c5e8d7ffe923ff307c3e8c7e@mail.gmail.com <55083236.8070900@2ndquadrant.com> <55083D74.4000601@2ndquadrant.com> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 17.3.2015 16:24, Thomas Kellerer wrote: > Tomas Vondra schrieb am 17.03.2015 um 15:43: >> On 17.3.2015 15:19, Thomas Kellerer wrote: >>> Tomas Vondra schrieb am 17.03.2015 um 14:55: >>>> (2) using window functions, e.g. like this: >>>> >>>> SELECT * FROM ( >>>> SELECT *, >>>> ROW_NUMBER() OVER (PARTITION BY touchpoint_execution_id >>>> ORDER BY FROM max_creation_dt) AS rn >>>> FROM s_f_touchpoint_execution_status_history >>>> ) foo WHERE rn = 1 >>>> >>>> But estimating this is also rather difficult ... >>> >>> >>> From my experience rewriting something like the above using DISTINCT >>> ON is usually faster. >> >> How do you get the last record (with respect to a timestamp column) >> using a DISTINCT ON? > > You need to use "order by ... desc". See here: http://sqlfiddle.com/#!15/d4846/2 Nice, thanks! > > Btw: your row_number() usage wouldn't return the "latest" row either. > It would return the "oldest" row. Oh, right. I forgot the DESC in the window. -- Tomas Vondra http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance