QUERY PLAN
Sort (cost=1989.54..1989.55 rows=1 width=202)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=695.29..1989.53 rows=1 width=202)
Group Key: c.id
-> Nested Loop Left Join (cost=695.29..1989.52 rows=1 width=194)
Join Filter: (cs.object_id = c.id)
-> Nested Loop Left Join (cost=695.29..1344.51 rows=1 width=194)
Join Filter: (rs.object_id = c.id)
-> Nested Loop Left Join (cost=695.29..705.64 rows=1 width=194)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Nested Loop (cost=695.29..703.53 rows=1 width=198)
-> Merge Left Join (cost=695.01..695.23 rows=1 width=194)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=68.80..68.80 rows=1 width=194)
Sort Key: c.id
-> Seq Scan on tbl_sales_contracts c (cost=0.00..68.79 rows=1 width=194)
Filter: ((erp_un_settle_amount > '0'::numeric) AND (client_id = 498))
-> Sort (cost=626.21..626.36 rows=58 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..624.51 rows=58 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 = 498)
-> 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..624.51 rows=1149 width=4)
Filter: (object_type = 106)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..624.51 rows=1640 width=4)
Filter: (object_type = 105)