Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxHJv-00011I-Jc for pgsql-performance@arkaria.postgresql.org; Mon, 12 Aug 2019 20:53:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hxHJt-00014m-Rl for pgsql-performance@arkaria.postgresql.org; Mon, 12 Aug 2019 20:53:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxHJt-00013x-Fl for pgsql-performance@lists.postgresql.org; Mon, 12 Aug 2019 20:53:33 +0000 Received: from mail49225.hm1479.locaweb.com.br ([201.76.49.225]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1hxHJo-00060P-6w for pgsql-performance@lists.postgresql.org; Mon, 12 Aug 2019 20:53:30 +0000 Received: from mcbain0017.correio.biz (201.76.49.40) by mail4970.hm1479.locaweb.com.br id ha78r816a4c2 for ; Mon, 12 Aug 2019 17:53:02 -0300 (envelope-from ) Received: from proxy.email-ssl.com.br (bartf0031.email.locaweb.com.br [10.31.120.63]) by mcbain0017.correio.biz (Postfix) with ESMTP id D14E87C0301 for ; Mon, 12 Aug 2019 17:53:25 -0300 (-03) x-locaweb-id: qyHzKK_R8aWhjwoIerpCUhsi5PfCUymhK53qXIAegdcRogeYX3-XTadcSpgGGxitWQf3rKGbFuqvLmn5XdJ_XeDnSC7FYZn7HbLXpGF-9IgCFmgQGgrxCDEo15F9LB44OnsLfXjEQNdayh8qp-s9AotEt2KUQ2RJBl9oRDY6_jQGONTy23UbTLyu956pUuVT8UlUgaIM6XARSVi84RGoZ3y_BYb-aN8PXpaI7MRvykI= NmM3NTY5NzM3MjZmNjI2NTcyNzQ2ZjQwNzM2OTczNjM2ZjYyNzI2MTJlNjM2ZjZkMmU2Mjcy X-LocaWeb-COR: locaweb_2009_x-mail X-AuthUser: luisroberto@siscobra.com.br Received: from [192.168.0.42] (unknown [189.45.198.123]) (Authenticated sender: luisroberto@siscobra.com.br) by proxy.email-ssl.com.br (Postfix) with ESMTPSA id 6624BB00224 for ; Mon, 12 Aug 2019 17:53:20 -0300 (-03) To: "pgsql-performance@lists.postgresql.org" From: =?UTF-8?Q?Lu=c3=ads_Roberto_Weck?= Subject: Last event per user Message-ID: Date: Mon, 12 Aug 2019 17:57:46 -0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: pt-BR List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hey guys, So I have two tables: users and events. It is very common for my application to request the last user event. Usually, what I'll do is get the user, and then SELECT * from events WHERE user_id = :user order by timestamp_inc desc LIMIT 1. I have a big problem, however: My app uses a ORM for SQL execution and generation and it cant create subselects at all. The Ideal solution for me would be a view which has all the users last events. I tried: creating a view (last_user_event_1) on "SELECT DISTINCT ON (user_id) * FROM events ORDER BY user_id, timestamp_inc DESC" and another one (last_user_event_2) which is a view on users with a lateral join on the last event. Running the query with lateral join by itself is very fast, and exactly what I need. It usually runs < 1ms. The one with "distinct on (user_id)" takes around 20ms to complete which is just too slow for my needs. My problem is that when I run a query JOINing users with last_user_event_2, it takes about 2 seconds: This is the explain output from joining users with "last_user_event_2": https://explain.depesz.com/s/oyEp And this is with "last_user_event_1": https://explain.depesz.com/s/hWwF Any help would be greatly appreciated.