QUERY PLAN
Limit (cost=1638.50..1638.55 rows=20 width=202)
-> Sort (cost=1638.50..1638.62 rows=47 width=202)
Sort Key: (CASE WHEN (c.expire_date IS NULL) THEN 1 ELSE 2 END) DESC, c.expire_date DESC
-> Group (cost=1636.42..1637.25 rows=47 width=202)
Group Key: c.id
-> Merge Left Join (cost=1636.42..1636.78 rows=47 width=194)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=1202.56..1202.68 rows=47 width=194)
Sort Key: c.id
-> Nested Loop (cost=759.47..1201.26 rows=47 width=194)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.73 rows=1 width=4)
Filter: ((sales_id = 1001) AND (group_id = 1))
-> Hash Right Join (cost=759.47..1199.05 rows=47 width=198)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..433.06 rows=1714 width=4)
Filter: (object_type = 105)
-> Hash (cost=758.88..758.88 rows=47 width=198)
-> Hash Right Join (cost=321.27..758.88 rows=47 width=198)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..433.06 rows=1198 width=4)
Filter: (object_type = 106)
-> Hash (cost=320.68..320.68 rows=47 width=198)
-> Hash Join (cost=89.67..320.68 rows=47 width=198)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..226.84 rows=1584 width=194)
-> Hash (cost=88.93..88.93 rows=59 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=4.73..88.93 rows=59 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.72 rows=59 width=0)
Index Cond: (sales_rep = 1001)
-> Sort (cost=433.86..433.94 rows=32 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..433.06 rows=32 width=4)
Filter: (object_type = 107)