Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dyePP-0000xY-Ee for pgsql-performance@arkaria.postgresql.org; Sun, 01 Oct 2017 13:35:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dyePO-0002mk-TH for pgsql-performance@arkaria.postgresql.org; Sun, 01 Oct 2017 13:35:50 +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 1dyePO-0002m7-7o for pgsql-performance@postgresql.org; Sun, 01 Oct 2017 13:35:50 +0000 Received: from mail.fmed.uba.ar ([157.92.152.1] helo=azteca.fmed.uba.ar) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dyePE-0007cj-Gr for pgsql-performance@postgresql.org; Sun, 01 Oct 2017 13:35:48 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id C48431F60FDE; Sun, 1 Oct 2017 10:35:48 -0300 (ART) Received: from azteca.fmed.uba.ar ([127.0.0.1]) by localhost (azteca.fmed.uba.ar [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id DfQhCXejL3HK; Sun, 1 Oct 2017 10:35:48 -0300 (ART) Received: from localhost (localhost.localdomain [127.0.0.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id E7E1C1F60FE2; Sun, 1 Oct 2017 10:35:47 -0300 (ART) X-Virus-Scanned: amavisd-new at fmed.uba.ar Received: from azteca.fmed.uba.ar ([127.0.0.1]) by localhost (azteca.fmed.uba.ar [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id AJTShG74tLbx; Sun, 1 Oct 2017 10:35:47 -0300 (ART) Received: from azteca.fmed.uba.ar (azteca.fmed.uba.ar [157.92.152.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id BFE3C1F60FDE; Sun, 1 Oct 2017 10:35:47 -0300 (ART) Date: Sun, 1 Oct 2017 13:35:47 +0000 (UTC) From: Gerardo Herzig To: Mariel Cherkassky Cc: pgsql-performance@postgresql.org Message-ID: <58852558.43813.1506864947581.JavaMail.zimbra@fmed.uba.ar> In-Reply-To: References: Subject: Re: select with max functions MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [186.136.213.2] X-Mailer: Zimbra 8.0.9_GA_6191 (ZimbraWebClient - FF51 (Linux)/8.0.9_GA_6191) Thread-Topic: select with max functions Thread-Index: ZfZLrn6WKNXkBXvHtdWrC1zT1vlSfg== 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 ----- Mensaje original ----- > De: "Mariel Cherkassky" > Para: pgsql-performance@postgresql.org > Enviados: Domingo, 1 de Octubre 2017 9:41:37 > Asunto: [PERFORM] select with max functions > > 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; > > > QUERY PLAN > > --------------------------------------------------------------------------------------------------------- > GroupAggregate (cost=0.56..3250554784.13 rows=115111 width=18) > Group Key: ma.user_id > Filter: (count(*) > 1) > -> Index Scan using manuim_i_user_id on manuim ma > (cost=0.56..3250552295.59 rows=178324 width=10) > Filter: (bb_open_date = (SubPlan 1)) > SubPlan 1 > -> Aggregate (cost=90.98..90.99 rows=1 width=8) > -> Index Scan using manuim_i_user_id on manuim man > (cost=0.56..90.92 rows=22 width=8) > Index Cond: ((user_id)::text = (ma.user_id)::text) > (9 rows) > > > > So I used the limit 1 option : > > 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; > > and the performance are still the same : > > QUERY PLAN > > --------------------------------------------------------------------------------------------------------------- > GroupAggregate (cost=0.56..3252248863.46 rows=115111 width=18) > Group Key: ma.user_id > Filter: (count(*) > 1) > -> Index Scan using manuim_i_user_id on manuim ma > (cost=0.56..3252246374.92 rows=178324 width=10) > Filter: (bb_open_date = (SubPlan 1)) > SubPlan 1 > -> Limit (cost=91.03..91.03 rows=1 width=8) > -> Sort (cost=91.03..91.09 rows=22 width=8) > Sort Key: man.bb_open_date DESC > -> Index Scan using manuim_i_user_id on manuim man > (cost=0.56..90.92 rows=22 width=8) > Index Cond: ((user_id)::text = > (ma.user_id)::text) > (11 rows) > > > > the reading on the table manuim takes a lot of effort, what else can I do ? > the table`s size is 8G. > > select count(*) from manuim; > count > ---------- > 35664828 > (1 row) > > the indexes on the table : > "manuim_bb_open_date" btree (bb_open_date) > "manuim_i_user_id" btree (user_id) > > > Any idea how can I continue from here ? Thanks , Mariel. Start by posting the results of "explain analyze" of that queries, so we can see some timming stuff. Gerardo -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance