QUERY PLAN
Limit (cost=451.41..451.41 rows=2 width=115)
-> Sort (cost=451.41..451.41 rows=2 width=115)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=451.39..451.40 rows=2 width=115)
Group Key: c.id
-> Sort (cost=451.39..451.39 rows=2 width=115)
Sort Key: c.id
-> Nested Loop Left Join (cost=60.55..451.38 rows=2 width=115)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Nested Loop Left Join (cost=60.55..448.38 rows=2 width=119)
Join Filter: (pcr.client_id = client.id)
Filter: ((client.sales_rep = ANY ('{5295,5311}'::integer[])) OR (client.sales_rep IS NULL) OR (hashed SubPlan 1) OR (pcr.id IS NULL))
-> Hash Right Join (cost=48.86..428.63 rows=2 width=123)
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=48.84..48.84 rows=2 width=123)
-> Nested Loop (cost=0.28..48.84 rows=2 width=123)
-> Seq Scan on tbl_purchase_client_rels pcr (cost=0.00..32.24 rows=2 width=12)
Filter: (client_id = 3332)
-> Index Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts c (cost=0.28..8.29 rows=1 width=115)
Index Cond: (id = pcr.contract_id)
-> Materialize (cost=0.28..8.30 rows=1 width=8)
-> Index Scan using tbl_clients_pkey on tbl_clients client (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = 3332)
SubPlan 1
-> Seq Scan on tbl_user_visit_clients (cost=0.00..11.41 rows=1 width=4)
Filter: (user_id = 5295)
-> Materialize (cost=0.00..1.71 rows=47 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.47 rows=47 width=4)