Re: Optimizing execution of expensive subqueries

2018-07-14 Thread David Rowley
On 12 July 2018 at 02:24, Mathieu Fenniak wrote: > I'm currently looking at a query that is generally selecting a bunch of > simple columns from a table, and also performing some subqueries to > aggregate related data, and then sorting by one of the simple columns and > paginating the result. > >

Re: Optimizing execution of expensive subqueries

2018-07-13 Thread Mathieu Fenniak
Hi Hellmuth, Thanks for the response and the new approach; a LATERAL JOIN is new to me. Unfortunately it seems to have the same performance characteristics and query plan. The aggregation in the lateral join still executes for every row (eg. if my base query has 50 rows, I get "Aggregate

Re: Optimizing execution of expensive subqueries

2018-07-11 Thread Hellmuth Vargas
Hi Try this way: SELECT tbl.field1, tbl.field2, tbl.field3, ..., b.Thingy1Sum, ... repeat for multiply thingies ... FROM tbl LATERAL JOIN ( SELECT anothertbl.UserId,SUM(Duration) as Thingy1Sum FROM anothertbl WHERE anothertbl.UserId = tbl.UserId AND anothertbl.ThingyId = 1 group by 1)

Optimizing execution of expensive subqueries

2018-07-11 Thread Mathieu Fenniak
Hi pgsql-general! I'm currently looking at a query that is generally selecting a bunch of simple columns from a table, and also performing some subqueries to aggregate related data, and then sorting by one of the simple columns and paginating the result. eg. SELECT tbl.field1, tbl.field2,