QUERY PLAN
Sort (cost=33.85..33.85 rows=1 width=43)
Sort Key: p.bargain_date DESC
-> HashAggregate (cost=33.83..33.84 rows=1 width=43)
Group Key: p.id
-> Hash Right Join (cost=12.84..33.61 rows=87 width=43)
Hash Cond: (r.erp_profit_distribute_id = t.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..17.80 rows=780 width=4)
-> Hash (cost=12.83..12.83 rows=1 width=47)
-> Nested Loop Left Join (cost=4.30..12.83 rows=1 width=47)
Join Filter: (t.project_id = p.id)
Filter: ((t.project_id IS NULL) OR (p.id = 3786))
-> Bitmap Heap Scan on tbl_projects p (cost=4.30..11.60 rows=1 width=43)
Recheck Cond: (client_id = 3334)
Filter: (project_status = 3)
-> Bitmap Index Scan on tbl_projects_client_id_idx (cost=0.00..4.29 rows=2 width=0)
Index Cond: (client_id = 3334)
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..1.12 rows=7 width=8)
Filter: ((project_id IS NOT NULL) AND (status = ANY ('{2,5,1}'::integer[])))