Re: Retrieve most recent 1 record from joined table
От | Thomas Kellerer |
---|---|
Тема | Re: Retrieve most recent 1 record from joined table |
Дата | |
Msg-id | ltejbl$s1k$1@ger.gmane.org обсуждение исходный текст |
Ответ на | Retrieve most recent 1 record from joined table (agharta <agharta82@gmail.com>) |
Список | pgsql-sql |
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
В списке pgsql-sql по дате отправления: