QUERY PLAN
Aggregate (cost=511.14..511.15 rows=1 width=8)
-> Sort (cost=511.11..511.11 rows=2 width=423)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=511.09..511.10 rows=2 width=423)
Group Key: c.id
-> Sort (cost=511.09..511.09 rows=2 width=16)
Sort Key: c.id
-> Nested Loop Left Join (cost=121.64..511.08 rows=2 width=16)
-> Nested Loop Left Join (cost=121.50..510.04 rows=2 width=20)
-> Nested Loop (cost=121.22..509.03 rows=2 width=20)
-> Index Only Scan using tbl_suppliers_pkey on tbl_suppliers supplier (cost=0.28..8.29 rows=1 width=4)
Index Cond: (id = 431)
-> Hash Right Join (cost=120.95..500.71 rows=2 width=24)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..372.94 rows=1819 width=4)
Filter: (object_type = 108)
-> Hash (cost=120.92..120.92 rows=2 width=24)
-> Hash Right Join (cost=88.45..120.92 rows=2 width=24)
Hash Cond: (pcr.contract_id = c.id)
-> Seq Scan on tbl_purchase_client_rels pcr (cost=0.00..27.79 rows=1779 width=8)
-> Hash (cost=88.42..88.42 rows=2 width=20)
-> Seq Scan on tbl_purchase_contracts c (cost=0.00..88.42 rows=2 width=20)
Filter: (supplier_id = 431)
-> Index Scan using tbl_clients_pkey on tbl_clients client (cost=0.28..0.51 rows=1 width=8)
Index Cond: (pcr.client_id = id)
-> Index Only Scan using tbl_sales_group_relationships_pkey on tbl_sales_group_relationships sgr (cost=0.14..0.51 rows=1 width=4)
Index Cond: (sales_id = client.sales_rep)