QUERY PLAN
Aggregate (cost=26.91..26.92 rows=1 width=8)
-> Unique (cost=26.63..26.77 rows=11 width=22)
-> Sort (cost=26.63..26.66 rows=11 width=22)
Sort Key: c.id, c.client_class, cc.notice_days, ((date_part('epoch'::text, (now() - max(sl.end_at))) / '86400'::double precision))
-> GroupAggregate (cost=26.09..26.44 rows=11 width=22)
Group Key: c.id, cc.id
Filter: ((date_part('epoch'::text, (now() - max(sl.end_at))) / '86400'::double precision) > (cc.notice_days)::double precision)
-> Sort (cost=26.09..26.11 rows=11 width=22)
Sort Key: c.id, cc.id
-> Nested Loop Left Join (cost=4.45..25.90 rows=11 width=22)
-> Nested Loop Left Join (cost=0.28..16.38 rows=1 width=14)
-> Index Scan using tbl_clients_sales_rep_idx on tbl_clients c (cost=0.14..8.15 rows=1 width=8)
Index Cond: (sales_rep = 5495)
Filter: (NOT deleted)
-> Index Scan using tbl_client_classes_pkey on tbl_client_classes cc (cost=0.14..8.16 rows=1 width=6)
Index Cond: (id = c.client_class)
-> Bitmap Heap Scan on tbl_work_logs sl (cost=4.16..9.50 rows=1 width=12)
Recheck Cond: (client_id = c.id)
Filter: (NOT is_draft)
-> Bitmap Index Scan on tbl_work_logs_client_id_idx (cost=0.00..4.16 rows=2 width=0)
Index Cond: (client_id = c.id)