Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ufepo-0000m5-89 for pgsql-performance@arkaria.postgresql.org; Thu, 23 May 2013 23:22:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Ufepn-0008Vk-JA for pgsql-performance@arkaria.postgresql.org; Thu, 23 May 2013 23:22:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ufepl-0008Uc-SY for pgsql-performance@postgresql.org; Thu, 23 May 2013 23:22:09 +0000 Received: from avasout04.plus.net ([212.159.14.19]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Ufeph-0002HA-RX for pgsql-performance@postgresql.org; Thu, 23 May 2013 23:22:09 +0000 Received: from server.barnhouse ([91.125.223.9]) by avasout04 with smtp id fbN31l0010CmrYj01bN4Vj; Fri, 24 May 2013 00:22:04 +0100 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=VLCbEQsZnkyOnq6j8E1AzA==:117 a=VLCbEQsZnkyOnq6j8E1AzA==:17 a=0Bzu9jTXAAAA:8 a=2YU7Ix1lbqoA:10 a=98iZN8NyO30A:10 a=ihvODaAuJD4A:10 a=EBOSESyhAAAA:8 a=lAG8KFRD8mEA:10 a=r77TgQKjGQsHNAKrUKIA:9 a=s-uuFgcD-ne8TDCSG7kA:9 a=wPNLvfGTeEIA:10 a=pGLkceISAAAA:8 a=6hE2MpUq97b7W5TBFT0A:9 a=VDiIovvdH-J83gBS:21 a=_W_S_7VecoQA:10 Received: from [192.168.0.96] (workstation.barnhouse [192.168.0.96]) by server.barnhouse (Postfix) with ESMTP id 8B87A6805C2; Fri, 24 May 2013 00:22:02 +0100 (BST) Message-ID: <519EA49B.7030404@mansionfamily.plus.com> Date: Fri, 24 May 2013 00:22:03 +0100 From: james Reply-To: james@mansionfamily.plus.com User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: Jonathan Morra CC: pgsql-performance@postgresql.org Subject: Re: Performance of complicated query References: <519E563A.4010401@pinpointresearch.com> <519E75AC.80103@pinpointresearch.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------000306010605030907080109" 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 This is a multi-part message in MIME format. --------------000306010605030907080109 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 23/05/2013 22:57, Jonathan Morra wrote: > I'm not sure I understand your proposed solution. There is also the > case to consider where the same patient can be assigned the same > device multiple times. In this case, the value may be reset at each > assignment (hence the line value - issued_value AS value from the > original query). > Perhaps you could use triggers to help somewhat? At least for the lifetime part. For a given assignment of a device to a patient, only the last value is useful, so you can maintain that easily enough (a bit like a materialised view but before 9.3 I guess). But, that might fix 'lifetime' but not some arbitrary windowed view. I can see why an 'as at' end time is useful, but not why a start time is so useful: if a device has readings before the window but not in the window, is that 'no reading' or should the last reading prior to the window apply? It also seems to me that the solution you have is hard to reason about. Its like a Haskell program done in one big inline fold rather than a bunch of 'where' clauses, and I find these cause significant brain overload. Perhaps you could break it out into identifiable chunks that work out (both for lifetime if not using triggers, and for your date range otherwise) the readings that are not superceded (ie the last in the date bounds for a device assignment), and then work with those. Consider the CTE 'WITH queries' for doing this? It seems to me that if you can do this, then the problem might be easier to express. Failing that, I'd be looking at using temporary tables, and forcing a series of reduce steps using them, but then I'm a nasty old Sybase hacker at heart. ;-) --------------000306010605030907080109 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 23/05/2013 22:57, Jonathan Morra wrote:
I'm not sure I understand your proposed solution.  There is also the case to consider where the same patient can be assigned the same device multiple times.  In this case, the value may be reset at each assignment (hence the line value - issued_value AS value from the original query).


Perhaps you could use triggers to help somewhat?  At least for the lifetime part.

For a given assignment of a device to a patient, only the last value is useful, so you can maintain that easily enough (a bit like a materialised view but before 9.3 I guess).

But, that might fix 'lifetime' but not some arbitrary windowed view.  I can see why an 'as at' end time is useful, but not why a start time is so useful: if a device has readings before the window but not in the window, is that 'no reading' or should the last reading prior to the window apply?

It also seems to me that the solution you have is hard to reason about.  Its like a Haskell program done in one big inline fold rather than a bunch of 'where' clauses, and I find these cause significant brain overload.

Perhaps you could break it out into identifiable chunks that work out (both for lifetime if not using triggers, and for your date range otherwise) the readings that are not superceded (ie the last in the date bounds for a device assignment), and then work with those.  Consider the CTE 'WITH queries' for doing this?

It seems to me that if you can do this, then the problem might be easier to express.

Failing that, I'd be looking at using temporary tables, and forcing a series of reduce steps using them, but then I'm a nasty old Sybase hacker at heart. ;-)

--------------000306010605030907080109--