Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XLnJf-0002Re-P8 for pgsql-sql@arkaria.postgresql.org; Mon, 25 Aug 2014 05:59:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XLnJe-000199-C8 for pgsql-sql@arkaria.postgresql.org; Mon, 25 Aug 2014 05:59:42 +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 1XLnJd-000192-Jh for pgsql-sql@postgresql.org; Mon, 25 Aug 2014 05:59:41 +0000 Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XLnJV-0006Fu-08 for pgsql-sql@postgresql.org; Mon, 25 Aug 2014 05:59:39 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XLnJQ-0003av-Fm for pgsql-sql@postgresql.org; Mon, 25 Aug 2014 07:59:28 +0200 Received: from 217.110.94.121 ([217.110.94.121]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 25 Aug 2014 07:59:28 +0200 Received: from spam_eater by 217.110.94.121 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 25 Aug 2014 07:59:28 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: Retrieve most recent 1 record from joined table Date: Mon, 25 Aug 2014 07:59:18 +0200 Lines: 31 Message-ID: References: <53F6F9DA.4060005@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.121 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: <53F6F9DA.4060005@gmail.com> X-Pg-Spam-Score: 0.6 (/) 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 agharta schrieb am 22.08.2014 um 10:05: > Joining the tables, how to get ONLY most recent record per table3(t3_date)?? > > Query example: > > select * from table1 as t1 > inner join table2 t2 on (t1.t1_id = t2.t1_id and t2.t2_value like('%ab%') ) > inner join table3 t3 on (t2.t2_id = t3.t2_id and t3.t3_date <= timestamp '2014-08-20') > order by t3.t2_id, t3.t3_date desc > This seems to be slightly faster, especially with the following index: create index idx_t3_combined on table3 (t2_id, t3_date desc, t3_id); select * from table1 as t1 join table2 t2 on t1.t1_id = t2.t1_id and t2.t2_value like '%ab%' join ( select distinct on (t2_id) t3_id, t3_date, t2_id from table3 order by t2_id, t3_date desc ) t3 on t3.t2_id = t2.t2_id order by t3.t2_id, t3.t3_date desc ; I also had to increase the work_mem in order to avoid disk based sorting for the joins -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql