I am trying to optimize this query:
SELECT eq.*,
reg_last_dt.dt as reg_last_date
FROM Equipment eq
INNER JOIN (
select max( dt ) as dt, id_eq_equipment
from consum
group by id_eq_equipment
) as reg_last_dt ON reg_last_dt.id_eq_equipment = eq.id_eq
Explain shows me this:
Hash Join (cost=839806.69..839833.33 rows=23 width=1461)
Hash Cond: (eq.id_eq = consum.id_eq_equipment)
-> Seq Scan on equipment eq (cost=0.00..26.29 rows=129 width=1453)
-> Hash (cost=839806.40..839806.40 rows=23 width=10)
-> Finalize GroupAggregate (cost=839805.60..839806.17 rows=23 width=10)
Group Key: consum.id_eq_equipment
-> Sort (cost=839805.60..839805.71 rows=46 width=10)
Sort Key: consum.id_eq_equipment
-> Gather (cost=839799.50..839804.33 rows=46 width=10)
Workers Planned: 2
-> Partial HashAggregate (cost=838799.50..838799.73 rows=23 width=10)
Group Key: consum.id_eq_equipment
-> Parallel Seq Scan on consum (cost=0.00..755192.33 rows=16721433 width=10)
This looks not very optimal. Is there anything I could do to make it better?