QUERY PLAN
Limit (cost=4634.53..4638.48 rows=20 width=569)
-> Result (cost=4634.53..4645.98 rows=58 width=569)
-> Sort (cost=4634.53..4634.67 rows=58 width=601)
Sort Key: (CASE WHEN (c.sales_rep IS NULL) THEN 0 ELSE 1 END) DESC, ((date_part('epoch'::text, (now() - max(tbl_work_logs.end_at))) / '86400'::double precision)) DESC NULLS LAST, c.created_at DESC
-> GroupAggregate (cost=4630.66..4632.98 rows=58 width=601)
Group Key: c.id, cl.id, (CASE WHEN (tbl_projects.client_id IS NULL) THEN false ELSE true END), ((date_part('epoch'::text, (now() - max(tbl_work_logs.end_at))) / '86400'::double precision)), (max(tbl_work_logs.end_at))
-> Sort (cost=4630.66..4630.81 rows=58 width=573)
Sort Key: c.id, cl.id, (CASE WHEN (tbl_projects.client_id IS NULL) THEN false ELSE true END), ((date_part('epoch'::text, (now() - max(tbl_work_logs.end_at))) / '86400'::double precision)), (max(tbl_work_logs.end_at))
-> Nested Loop Left Join (cost=2380.51..4628.97 rows=58 width=573)
-> Nested Loop Left Join (cost=2380.22..4190.41 rows=58 width=576)
Join Filter: (cl.id = c.client_class)
-> Nested Loop Left Join (cost=2380.22..4186.20 rows=58 width=532)
Join Filter: (sgr.sales_id = c.sales_rep)
-> Hash Right Join (cost=2380.22..4183.69 rows=58 width=532)
Hash Cond: (arrive.client_id = c.id)
-> Seq Scan on tbl_work_logs arrive (cost=0.00..1802.64 rows=209 width=12)
Filter: ((NOT is_draft) AND (location_in_id IS NOT NULL) AND (location_out_id IS NOT NULL) AND (client_id IS NOT NULL) AND (scenario_id = 1))
-> Hash (cost=2379.49..2379.49 rows=58 width=524)
-> Hash Right Join (cost=2337.35..2379.49 rows=58 width=524)
Hash Cond: (tbl_work_logs.client_id = c.id)
-> HashAggregate (cost=1980.95..2005.71 rows=1238 width=20)
Group Key: tbl_work_logs.client_id
-> Seq Scan on tbl_work_logs (cost=0.00..1713.31 rows=35685 width=12)
Filter: (NOT is_draft)
-> Hash (cost=355.68..355.68 rows=58 width=508)
-> Hash Right Join (cost=318.18..355.68 rows=58 width=508)
Hash Cond: (car.client_id = c.id)
-> Seq Scan on tbl_client_address_rels car (cost=0.00..29.88 rows=1888 width=8)
-> Hash (cost=317.45..317.45 rows=58 width=508)
-> Hash Right Join (cost=297.84..317.45 rows=58 width=508)
Hash Cond: (tbl_projects.client_id = c.id)
-> HashAggregate (cost=188.50..197.17 rows=867 width=4)
Group Key: tbl_projects.client_id
-> Seq Scan on tbl_projects (cost=0.00..184.28 rows=1691 width=4)
Filter: (project_status = 3)
-> Hash (cost=108.61..108.61 rows=58 width=504)
-> Bitmap Heap Scan on tbl_clients c (cost=14.46..108.61 rows=58 width=504)
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)
-> Materialize (cost=0.00..1.64 rows=1 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.64 rows=1 width=4)
Filter: (sales_id = 1001)
-> Materialize (cost=0.00..1.06 rows=4 width=44)
-> Seq Scan on tbl_client_classes cl (cost=0.00..1.04 rows=4 width=44)
-> Index Only Scan using tbl_term_taxonomy_relationships_object_id_term_id_idx on tbl_term_taxonomy_relationships ttr (cost=0.29..7.55 rows=1 width=4)
Index Cond: ((object_id = c.id) AND (term_id = 14))