QUERY PLAN
Aggregate (cost=2046.54..2046.55 rows=1 width=8)
-> Sort (cost=2046.52..2046.53 rows=1 width=1067)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=716.08..2046.51 rows=1 width=1067)
Group Key: c.id
-> Nested Loop Left Join (cost=716.08..2046.51 rows=1 width=16)
Join Filter: (cs.object_id = c.id)
-> Nested Loop Left Join (cost=716.08..1383.26 rows=1 width=16)
Join Filter: (rs.object_id = c.id)
-> Nested Loop Left Join (cost=716.08..726.37 rows=1 width=16)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Nested Loop (cost=716.08..724.27 rows=1 width=20)
-> Merge Left Join (cost=715.80..715.97 rows=1 width=20)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=72.08..72.08 rows=1 width=24)
Sort Key: c.id
-> Seq Scan on tbl_sales_contracts c (cost=0.00..72.06 rows=1 width=24)
Filter: ((erp_un_settle_amount > '0'::numeric) AND (client_id = 1301))
-> Sort (cost=643.73..643.84 rows=43 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..642.56 rows=43 width=4)
Filter: (object_type = 107)
-> Index Scan using tbl_clients_pkey on tbl_clients client (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = 1301)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.49 rows=49 width=4)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..642.56 rows=1146 width=4)
Filter: (object_type = 106)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..642.56 rows=1655 width=4)
Filter: (object_type = 105)