QUERY PLAN
Aggregate (cost=213.69..213.70 rows=1 width=8)
-> Sort (cost=213.63..213.64 rows=4 width=623)
Sort Key: c.signed_at DESC, c.created_at DESC
-> HashAggregate (cost=213.55..213.59 rows=4 width=623)
Group Key: c.id
-> Hash Left Join (cost=122.94..196.11 rows=6977 width=16)
Hash Cond: (pcr.client_id = client.id)
Filter: ((client.sales_rep = ANY ('{5495,5493}'::integer[])) OR (client.sales_rep IS NULL) OR (hashed SubPlan 1) OR (pcr.id IS NULL))
-> Hash Right Join (cost=34.32..70.13 rows=1233 width=24)
Hash Cond: (pcr.contract_id = c.id)
-> Seq Scan on tbl_purchase_client_rels pcr (cost=0.00..28.50 rows=1850 width=12)
-> Hash (cost=34.27..34.27 rows=4 width=16)
-> Hash Right Join (cost=1.12..34.27 rows=4 width=16)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..33.12 rows=9 width=4)
Filter: (object_type = 108)
-> Hash (cost=1.07..1.07 rows=4 width=20)
-> Seq Scan on tbl_purchase_contracts c (cost=0.00..1.07 rows=4 width=20)
Filter: (cost_type = 1)
-> Hash (cost=50.54..50.54 rows=204 width=8)
-> Hash Right Join (cost=10.45..50.54 rows=204 width=8)
Hash Cond: (sgr.sales_id = client.sales_rep)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..30.40 rows=2040 width=4)
-> Hash (cost=10.20..10.20 rows=20 width=8)
-> Seq Scan on tbl_clients client (cost=0.00..10.20 rows=20 width=8)
SubPlan 1
-> Seq Scan on tbl_user_visit_clients (cost=0.00..35.50 rows=10 width=4)
Filter: (user_id = 5495)