QUERY PLAN
Limit (cost=121.49..121.50 rows=4 width=279)
-> Sort (cost=121.49..121.50 rows=4 width=279)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=112.67..121.45 rows=4 width=279)
Group Key: c.id
-> Nested Loop (cost=112.67..121.36 rows=34 width=247)
-> Merge Left Join (cost=112.52..112.76 rows=34 width=251)
Merge Cond: (c.id = cs.object_id)
-> Sort (cost=79.25..79.34 rows=34 width=251)
Sort Key: c.id
-> Hash Join (cost=44.82..78.39 rows=34 width=251)
Hash Cond: (pcr.contract_id = c.id)
-> Hash Join (cost=43.69..77.13 rows=51 width=8)
Hash Cond: (pcr.client_id = client.id)
Join Filter: ((client.sales_rep = ANY ('{5495,5493}'::integer[])) OR (client.sales_rep IS NULL) OR (hashed SubPlan 1) OR (pcr.id IS NULL))
-> Seq Scan on tbl_purchase_client_rels pcr (cost=0.00..28.50 rows=1850 width=12)
-> Hash (cost=8.15..8.15 rows=1 width=8)
-> Index Scan using tbl_clients_sales_rep_idx on tbl_clients client (cost=0.14..8.15 rows=1 width=8)
Index Cond: (sales_rep = 5495)
SubPlan 1
-> Seq Scan on tbl_user_visit_clients (cost=0.00..35.50 rows=10 width=4)
Filter: (user_id = 5495)
-> Hash (cost=1.07..1.07 rows=4 width=247)
-> Seq Scan on tbl_purchase_contracts c (cost=0.00..1.07 rows=4 width=247)
Filter: (cost_type = 1)
-> Sort (cost=33.27..33.29 rows=9 width=4)
Sort Key: cs.object_id
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..33.12 rows=9 width=4)
Filter: (object_type = 108)
-> Materialize (cost=0.15..8.18 rows=1 width=4)
-> Index Only Scan using tbl_sales_group_relationships_pkey on tbl_sales_group_relationships sgr (cost=0.15..8.17 rows=1 width=4)
Index Cond: ((group_id = 2) AND (sales_id = 5495))