QUERY PLAN
Aggregate (cost=2004.83..2004.84 rows=1 width=8)
-> Hash Join (cost=1915.96..2004.78 rows=22 width=0)
Hash Cond: (c.client_class = cc.id)
Join Filter: (((((date_part('epoch'::text, (CURRENT_TIMESTAMP - max(sl.end_at))) / '86400'::double precision)) IS NOT NULL) AND (((date_part('epoch'::text, (CURRENT_TIMESTAMP - max(sl.end_at))) / '86400'::double precision)) > (cc.notice_days)::double precision)) OR ((((date_part('epoch'::text, (CURRENT_TIMESTAMP - max(sl.end_at))) / '86400'::double precision)) IS NULL) AND ((date_part('epoch'::text, ((CURRENT_DATE)::timestamp with time zone - c.created_at)) / '86400'::double precision) > (cc.notice_days)::double precision)))
-> Hash Left Join (cost=1914.87..2003.41 rows=67 width=20)
Hash Cond: (c.id = sub_c.id)
-> Bitmap Heap Scan on tbl_clients c (cost=4.80..93.17 rows=67 width=16)
Recheck Cond: (sales_rep = 1001)
Filter: (NOT deleted)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)
-> Hash (cost=1909.23..1909.23 rows=67 width=12)
-> HashAggregate (cost=1907.22..1908.56 rows=67 width=12)
Group Key: sub_c.id
-> Hash Right Join (cost=94.01..1901.28 rows=1189 width=12)
Hash Cond: (sl.client_id = sub_c.id)
-> Seq Scan on tbl_work_logs sl (cost=0.00..1713.31 rows=35731 width=12)
-> Hash (cost=93.17..93.17 rows=67 width=4)
-> Bitmap Heap Scan on tbl_clients sub_c (cost=4.80..93.17 rows=67 width=4)
Recheck Cond: (sales_rep = 1001)
Filter: (NOT deleted)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)
-> Hash (cost=1.04..1.04 rows=4 width=6)
-> Seq Scan on tbl_client_classes cc (cost=0.00..1.04 rows=4 width=6)