QUERY PLAN
Aggregate (cost=2119.27..2119.28 rows=1 width=8)
-> Sort (cost=2117.53..2117.82 rows=116 width=553)
Sort Key: ((date_part('epoch'::text, (now() - max(sl.end_at))) / '86400'::double precision)), c.created_at DESC
-> HashAggregate (cost=2111.23..2113.55 rows=116 width=553)
Group Key: c.id, CASE WHEN (p.id IS NULL) THEN false ELSE true END
-> Hash Right Join (cost=283.12..2103.46 rows=1037 width=21)
Hash Cond: (sl.client_id = c.id)
-> Seq Scan on tbl_work_logs sl (cost=0.00..1680.24 rows=35024 width=12)
-> Hash (cost=282.39..282.39 rows=58 width=16)
-> Nested Loop (cost=95.62..282.39 rows=58 width=16)
-> 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=95.62..280.08 rows=58 width=20)
Hash Cond: (p.client_id = c.id)
-> Seq Scan on tbl_projects p (cost=0.00..180.19 rows=1623 width=8)
Filter: (project_status = 3)
-> Hash (cost=94.89..94.89 rows=58 width=16)
-> Bitmap Heap Scan on tbl_clients c (cost=4.73..94.89 rows=58 width=16)
Recheck Cond: (sales_rep = 1001)
Filter: (NOT deleted)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.71 rows=58 width=0)
Index Cond: (sales_rep = 1001)