QUERY PLAN
Limit (cost=5588.13..5590.11 rows=10 width=586)
-> Result (cost=5588.13..5895.64 rows=1557 width=586)
-> Sort (cost=5588.13..5592.03 rows=1557 width=618)
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)), c.created_at DESC
-> HashAggregate (cost=5523.35..5554.49 rows=1557 width=618)
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))
-> Hash Right Join (cost=4250.94..5496.10 rows=1557 width=590)
Hash Cond: (ttr.object_id = c.id)
-> Seq Scan on tbl_term_taxonomy_relationships ttr (cost=0.00..1234.66 rows=1829 width=4)
Filter: (term_id = 14)
-> Hash (cost=4231.48..4231.48 rows=1557 width=593)
-> Hash Left Join (cost=2437.34..4231.48 rows=1557 width=593)
Hash Cond: (c.id = tbl_work_logs.client_id)
-> Hash Left Join (cost=464.85..2254.89 rows=1557 width=577)
Hash Cond: (c.id = tbl_projects.client_id)
-> Hash Left Join (cost=257.68..2043.63 rows=1557 width=573)
Hash Cond: (c.client_class = cl.id)
-> Nested Loop Left Join (cost=256.59..2035.88 rows=1557 width=529)
Join Filter: (sgr.sales_id = c.sales_rep)
-> Hash Right Join (cost=256.59..2010.91 rows=1557 width=529)
Hash Cond: (arrive.client_id = c.id)
-> Seq Scan on tbl_work_logs arrive (cost=0.00..1747.80 rows=618 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=237.13..237.13 rows=1557 width=521)
-> Hash Right Join (cost=202.60..237.13 rows=1557 width=521)
Hash Cond: (car.client_id = c.id)
-> Seq Scan on tbl_client_address_rels car (cost=0.00..29.63 rows=1863 width=8)
-> Hash (cost=183.14..183.14 rows=1557 width=521)
-> Seq Scan on tbl_clients c (cost=0.00..183.14 rows=1557 width=521)
Filter: ((NOT deleted) AND (sales_rep = 1001))
-> Materialize (cost=0.00..1.62 rows=1 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.61 rows=1 width=4)
Filter: (sales_id = 1001)
-> Hash (cost=1.04..1.04 rows=4 width=44)
-> Seq Scan on tbl_client_classes cl (cost=0.00..1.04 rows=4 width=44)
-> Hash (cost=197.03..197.03 rows=811 width=4)
-> HashAggregate (cost=180.81..188.92 rows=811 width=4)
Group Key: tbl_projects.client_id
-> Seq Scan on tbl_projects (cost=0.00..176.85 rows=1582 width=4)
Filter: (project_status = 3)
-> Hash (cost=1957.23..1957.23 rows=1221 width=20)
-> HashAggregate (cost=1920.60..1945.02 rows=1221 width=20)
Group Key: tbl_work_logs.client_id
-> Seq Scan on tbl_work_logs (cost=0.00..1661.24 rows=34581 width=12)
Filter: (NOT is_draft)