QUERY PLAN
Aggregate (cost=9447.19..9447.20 rows=1 width=8)
-> Sort (cost=9369.16..9382.16 rows=5202 width=3873)
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
-> HashAggregate (cost=316.05..368.07 rows=5202 width=3873)
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 Left Join (cost=164.53..251.02 rows=5202 width=37)
Hash Cond: (c.id = ttr.object_id)
-> Hash Right Join (cost=147.87..214.07 rows=5202 width=40)
Hash Cond: (car.client_id = c.id)
-> Seq Scan on tbl_client_address_rels car (cost=0.00..30.40 rows=2040 width=8)
-> Hash (cost=147.23..147.23 rows=51 width=40)
-> Nested Loop Left Join (cost=92.28..147.23 rows=51 width=40)
Join Filter: (arrive.client_id = c.id)
-> Hash Right Join (cost=92.28..130.84 rows=51 width=40)
Hash Cond: (sgr.sales_id = c.sales_rep)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..30.40 rows=2040 width=4)
-> Hash (cost=92.22..92.22 rows=5 width=40)
-> Hash Left Join (cost=78.72..92.22 rows=5 width=40)
Hash Cond: (c.id = tbl_projects.client_id)
-> Hash Right Join (cost=67.30..80.79 rows=5 width=36)
Hash Cond: (cl.id = c.client_class)
-> Seq Scan on tbl_client_classes cl (cost=0.00..12.50 rows=250 width=4)
-> Hash (cost=67.24..67.24 rows=5 width=36)
-> Hash Right Join (cost=62.07..67.24 rows=5 width=36)
Hash Cond: (tbl_work_logs.client_id = c.id)
-> HashAggregate (cost=16.19..19.35 rows=158 width=20)
Group Key: tbl_work_logs.client_id
-> Seq Scan on tbl_work_logs (cost=0.00..14.50 rows=225 width=12)
Filter: (NOT is_draft)
-> Hash (cost=45.82..45.82 rows=5 width=20)
-> Seq Scan on tbl_clients c (cost=35.52..45.82 rows=5 width=20)
Filter: ((NOT deleted) AND ((sales_rep = 5493) OR (sales_rep IS NULL) OR (hashed SubPlan 1)))
SubPlan 1
-> Seq Scan on tbl_user_visit_clients (cost=0.00..35.50 rows=10 width=4)
Filter: (user_id = 5495)
-> Hash (cost=11.40..11.40 rows=1 width=4)
-> Group (cost=11.38..11.39 rows=1 width=4)
Group Key: tbl_projects.client_id
-> Sort (cost=11.38..11.39 rows=1 width=4)
Sort Key: tbl_projects.client_id
-> Seq Scan on tbl_projects (cost=0.00..11.38 rows=1 width=4)
Filter: (project_status = 3)
-> Materialize (cost=0.00..15.63 rows=1 width=4)
-> Seq Scan on tbl_work_logs arrive (cost=0.00..15.62 rows=1 width=4)
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=16.62..16.62 rows=3 width=4)
-> Seq Scan on tbl_term_taxonomy_relationships ttr (cost=0.00..16.62 rows=3 width=4)
Filter: (term_id = 14)