QUERY PLAN
Limit (cost=129.65..133.60 rows=20 width=3873)
-> Result (cost=129.65..149.79 rows=102 width=3873)
-> Sort (cost=129.65..129.90 rows=102 width=3905)
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=124.89..126.93 rows=102 width=3905)
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=75.51..123.11 rows=102 width=3877)
Hash Cond: (c.id = ttr.object_id)
-> Nested Loop (cost=58.85..106.04 rows=102 width=3880)
-> Index Only Scan using tbl_sales_group_relationships_pkey on tbl_sales_group_relationships sgr (cost=0.15..8.17 rows=1 width=4)
Index Cond: ((group_id = 1) AND (sales_id = 5493))
-> Hash Right Join (cost=58.70..96.85 rows=102 width=3880)
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=58.69..58.69 rows=1 width=3880)
-> Nested Loop Left Join (cost=40.05..58.69 rows=1 width=3880)
-> Nested Loop Left Join (cost=35.88..49.16 rows=1 width=3872)
Join Filter: (tbl_projects.client_id = c.id)
-> Nested Loop Left Join (cost=24.50..37.75 rows=1 width=3868)
-> Hash Right Join (cost=24.36..29.52 rows=1 width=3824)
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=8.15..8.15 rows=1 width=3808)
-> Index Scan using tbl_clients_sales_rep_idx on tbl_clients c (cost=0.14..8.15 rows=1 width=3808)
Index Cond: (sales_rep = 5493)
Filter: (NOT deleted)
-> Index Scan using tbl_client_classes_pkey on tbl_client_classes cl (cost=0.14..8.16 rows=1 width=44)
Index Cond: (id = c.client_class)
-> 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)
-> Bitmap Heap Scan on tbl_work_logs arrive (cost=4.17..9.51 rows=1 width=12)
Recheck Cond: ((client_id = c.id) AND (client_id IS NOT NULL))
Filter: ((NOT is_draft) AND (location_in_id IS NOT NULL) AND (location_out_id IS NOT NULL) AND (scenario_id = 1))
-> Bitmap Index Scan on tbl_work_logs_client_id_idx (cost=0.00..4.17 rows=2 width=0)
Index Cond: ((client_id = c.id) AND (client_id IS NOT NULL))
-> 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)