Re: [PERFORM] select with max functions - Mailing list pgsql-performance

From Andreas Kretschmer
Subject Re: [PERFORM] select with max functions
Date
Msg-id 0970df65-602e-724d-2c39-e4695ae48bb6@a-kretschmer.de
Whole thread Raw
In response to [PERFORM] select with max functions  (Mariel Cherkassky <mariel.cherkassky@gmail.com>)
Responses Re: [PERFORM] select with max functions  (Mariel Cherkassky <mariel.cherkassky@gmail.com>)
List pgsql-performance

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

pgsql-performance by date:

Previous
From: Gerardo Herzig
Date:
Subject: Re: [PERFORM] select with max functions
Next
From: Mariel Cherkassky
Date:
Subject: Re: [PERFORM] select with max functions