Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dyjK3-0000FX-3d for pgsql-performance@arkaria.postgresql.org; Sun, 01 Oct 2017 18:50:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dyjK2-0007i2-JS for pgsql-performance@arkaria.postgresql.org; Sun, 01 Oct 2017 18:50:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dyjIH-0004YQ-Qs for pgsql-performance@postgresql.org; Sun, 01 Oct 2017 18:48:49 +0000 Received: from mailout01.ims-firmen.de ([213.174.32.96]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dyjIF-0002VW-0r for pgsql-performance@postgresql.org; Sun, 01 Oct 2017 18:48:49 +0000 Received: from mailin03.ims-firmen.de ([192.168.1.143]) by mailout01.ims-firmen.de with esmtp (envelope-from ) id 1dyjID-0002Dg-i2 for pgsql-performance@postgresql.org; Sun, 01 Oct 2017 20:48:45 +0200 Received: from [84.176.150.102] (helo=[192.168.222.106]) by mailin03.ims-firmen.de with esmtpa (envelope-from ) id 1dyjID-0002Tl-8B for pgsql-performance@postgresql.org; Sun, 01 Oct 2017 20:48:45 +0200 Subject: Re: select with max functions To: pgsql-performance@postgresql.org References: From: Andreas Kretschmer Message-ID: <0970df65-602e-724d-2c39-e4695ae48bb6@a-kretschmer.de> Date: Sun, 1 Oct 2017 20:48:45 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 Am 01.10.2017 um 14:41 schrieb Mariel Cherkassky: > Hi, > I need to use the max function in my query. I had very bad performance > when I used the max : > > SELECT Ma.User_Id, > COUNT(*) COUNT > FROM Manuim Ma > WHERE Ma.Bb_Open_Date = > (SELECT max(Bb_Open_Date) > FROM Manuim Man > WHERE Man.User_Id = Ma.User_Id > ) > GROUP BY Ma.User_Id > HAVING COUNT(*) > 1; > > > Any idea how can I continue from here ? Thanks , Mariel. Maybe you can rewrite it, for instance to select distinct on (user_id, bb_open_date) user_id, bb_open_date, count(1) from Manuim group by 1,2 having count(1) > 1; maybe much cheaper, but untested! If not, please share more details, at least table-definition. Regards, Andreas -- 2ndQuadrant - The PostgreSQL Support Company. www.2ndQuadrant.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance