QUERY PLAN
Aggregate (cost=1638.59..1638.60 rows=1 width=8)
-> Sort (cost=1637.88..1638.00 rows=47 width=1067)
Sort Key: (CASE WHEN (c.expire_date IS NULL) THEN 1 ELSE 2 END) DESC, c.expire_date DESC
-> Group (cost=1636.34..1636.58 rows=47 width=1067)
Group Key: c.id
-> Sort (cost=1636.34..1636.46 rows=47 width=8)
Sort Key: c.id
-> Nested Loop (cost=1193.25..1635.04 rows=47 width=8)
-> 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=1193.25..1632.83 rows=47 width=12)
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=1192.66..1192.66 rows=47 width=12)
-> Hash Right Join (cost=755.05..1192.66 rows=47 width=12)
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=754.46..754.46 rows=47 width=12)
-> Hash Right Join (cost=321.27..754.46 rows=47 width=12)
Hash Cond: (vs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..433.06 rows=32 width=4)
Filter: (object_type = 107)
-> Hash (cost=320.68..320.68 rows=47 width=12)
-> Hash Join (cost=89.67..320.68 rows=47 width=12)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..226.84 rows=1584 width=16)
-> 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)