sql slow postgresql dbeaver


I am using DBeaver to query a PostgreSQL database.
I have this query, it simply selects the highest id per Enterprise_Nbr. The query works but is really slow. Is there any way I can rewrite the query to improve performance.

I am using the querytool DBeaver because I don’t have direct access to PostgreSQL. The ultimate goal is to link the PostgreSQL with PowerBi.

select * 
from public.address 
where "ID"  in (select max("ID") 
  from public.address a 
  group by "Enterprise_Nbr")

>Solution :

Queries for problems are typically faster if done using Postgres’ proprietary distinct on () operator

select distinct on ("Enterprise_Nbr") *
from public.address
order by "Enterprise_Nbr", "ID" desc;

Leave a ReplyCancel reply