Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ufbhx-0003xW-8o for pgsql-performance@arkaria.postgresql.org; Thu, 23 May 2013 20:01:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Ufbhw-0004Si-JO for pgsql-performance@arkaria.postgresql.org; Thu, 23 May 2013 20:01:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ufbhv-0004Sc-IM for pgsql-performance@postgresql.org; Thu, 23 May 2013 20:01:51 +0000 Received: from cerberus.pinpointresearch.com ([66.7.238.130] helo=polaris.pinpointresearch.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Ufbhn-0006z7-HR for pgsql-performance@postgresql.org; Thu, 23 May 2013 20:01:51 +0000 Received: from [192.168.1.179] (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id ABFB5E00EC82; Thu, 23 May 2013 13:01:43 -0700 (PDT) Message-ID: <519E75AC.80103@pinpointresearch.com> Date: Thu, 23 May 2013 13:01:48 -0700 From: Steve Crawford User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: Jonathan Morra CC: pgsql-performance@postgresql.org Subject: Re: Performance of complicated query References: <519E563A.4010401@pinpointresearch.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -3.0 (---) 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 05/23/2013 10:57 AM, Jonathan Morra wrote: > Ultimately I'm going to deploy this to Heroku on a Linux machine (my > tests have so far indicated that Heroku is MUCH slower than my > machine), but I wanted to get it fast on my local machine first. I > agree with your role partitioning, however, this is only a dev machine. > > For the sum vs. last, the idea is that each patient is issued a device > and reads are recorded. The nature of the reads are that they are > incremental, so if a patient never changes devices there is no need > for a sum. However, patients will be changing devices, and the > patient_device table records when each patient had a given device. > What I want to sum up is the total value for a patient regardless of > how many times they changed devices If the reads are always incremented - that is the read you want is always the largest read - then something along these lines might work well and be more readable (untested code); -- distill out max value for each device with device_maxreads as ( select device_id, max(value) as max_read from reads group by device_id) -- then sum into a totals for each patient patient_value as ( select p.patient_id, sum(max_read) patient_value from device_maxreads d join patient_devices p on p.device_id = d.device_id group by p.patient_id ) select ...whatever... from ...your tables. join patient_value p on p.patient_id = ... ; If the values increment and decrement or patients are issued devices at overlapping times (i.e. using two devices at one time) then the query gets more complicated but "with..." is still a likely usable construct. Cheers, Steve -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance