QUERY PLAN
Limit (cost=1443.96..1443.98 rows=10 width=188)
-> Sort (cost=1443.96..1446.81 rows=1142 width=188)
Sort Key: c.signed_at DESC, c.created_at DESC
-> HashAggregate (cost=1399.29..1419.28 rows=1142 width=188)
Group Key: c.id
-> Hash Left Join (cost=1002.09..1396.44 rows=1142 width=180)
Hash Cond: (client.sales_rep = sgr.sales_id)
-> Hash Right Join (cost=1000.04..1384.21 rows=1142 width=184)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..376.24 rows=1535 width=4)
Filter: (object_type = 105)
-> Hash (cost=985.76..985.76 rows=1142 width=184)
-> Hash Right Join (cost=603.98..985.76 rows=1142 width=184)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..376.24 rows=1074 width=4)
Filter: (object_type = 106)
-> Hash (cost=589.70..589.70 rows=1142 width=184)
-> Hash Right Join (cost=213.31..589.70 rows=1142 width=184)
Hash Cond: (vs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..376.24 rows=29 width=4)
Filter: (object_type = 107)
-> Hash (cost=199.04..199.04 rows=1142 width=184)
-> Hash Left Join (cost=139.78..199.04 rows=1142 width=184)
Hash Cond: (c.client_id = client.id)
Filter: ((client.sales_rep = 1005) OR (client.sales_rep IS NULL) OR (hashed SubPlan 1))
-> Seq Scan on tbl_sales_contracts c (cost=0.00..55.24 rows=1524 width=180)
-> Hash (cost=104.83..104.83 rows=1883 width=8)
-> Seq Scan on tbl_clients client (cost=0.00..104.83 rows=1883 width=8)
SubPlan 1
-> Seq Scan on tbl_user_visit_clients (cost=0.00..11.41 rows=1 width=4)
Filter: (user_id = 1005)
-> Hash (cost=1.47..1.47 rows=47 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.47 rows=47 width=4)