Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dz2hQ-0001Z9-N4 for pgsql-performance@arkaria.postgresql.org; Mon, 02 Oct 2017 15:32:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dz2hQ-00027w-0c for pgsql-performance@arkaria.postgresql.org; Mon, 02 Oct 2017 15:32:04 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dz2ff-0007IZ-17 for pgsql-performance@postgresql.org; Mon, 02 Oct 2017 15:30:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dz2fX-0005me-QT for pgsql-performance@postgresql.org; Mon, 02 Oct 2017 15:30:13 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v92FTxh2016485; Mon, 2 Oct 2017 11:29:59 -0400 From: Tom Lane To: Mariel Cherkassky cc: Gerardo Herzig , pgsql-performance@postgresql.org Subject: Re: select with max functions In-reply-to: References: <0970df65-602e-724d-2c39-e4695ae48bb6@a-kretschmer.de> <1990398729.67887.1506951911360.JavaMail.zimbra@fmed.uba.ar> Comments: In-reply-to Mariel Cherkassky message dated "Mon, 02 Oct 2017 17:45:36 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16483.1506958199.1@sss.pgh.pa.us> Date: Mon, 02 Oct 2017 11:29:59 -0400 Message-ID: <16484.1506958199@sss.pgh.pa.us> 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 Mariel Cherkassky writes: > explain analyze SELECT Ma.User_Id, > COUNT(*) COUNT > FROM Manuim Ma > WHERE Ma.Bb_Open_Date = > (SELECT Bb_Open_Date > FROM Manuim Man > WHERE Man.User_Id = Ma.User_Id order > by bb_open_date desc limit 1 > ) > GROUP BY Ma.User_Id > HAVING COUNT(*) > 1; The core problem with this query is that the sub-select has to be done over again for each row of the outer table, since it's a correlated sub-select (ie, it refers to Ma.User_Id from the outer table). Replacing a max() call with handmade logic doesn't do anything to help that. I'd try refactoring it so that you calculate the max Bb_Open_Date just once for each user id, perhaps along the lines of SELECT Ma.User_Id, COUNT(*) COUNT FROM Manuim Ma, (SELECT User_Id, max(Bb_Open_Date) as max FROM Manuim Man GROUP BY User_Id) ss WHERE Ma.User_Id = ss.User_Id AND Ma.Bb_Open_Date = ss.max GROUP BY Ma.User_Id HAVING COUNT(*) > 1; This is still not going to be instantaneous, but it might be better. It's possible that an index on (User_Id, Bb_Open_Date) would help, but I'm not sure. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance