QUERY PLAN
Aggregate (cost=297.28..297.29 rows=1 width=8)
-> Sort (cost=281.98..284.53 rows=1020 width=3873)
Sort Key: (CASE WHEN (c.sales_rep IS NULL) THEN 0 ELSE 1 END) DESC
-> HashAggregate (cost=220.80..231.00 rows=1020 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=155.35..208.05 rows=1020 width=29)
Hash Cond: (c.id = ttr.object_id)
-> Hash Left Join (cost=138.68..187.42 rows=1020 width=32)
Hash Cond: (c.sales_rep = sgr.sales_id)
-> Hash Right Join (cost=104.55..142.70 rows=102 width=32)
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=104.54..104.54 rows=1 width=32)
-> Nested Loop Left Join (cost=85.90..104.54 rows=1 width=32)
-> Nested Loop Left Join (cost=81.73..95.01 rows=1 width=32)
Join Filter: (tbl_projects.client_id = c.id)
-> Nested Loop Left Join (cost=70.35..83.60 rows=1 width=28)
-> Hash Right Join (cost=70.20..75.37 rows=1 width=28)
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=54.01..54.01 rows=1 width=12)
-> Index Scan using tbl_clients_sales_rep_idx on tbl_clients c (cost=45.98..54.01 rows=1 width=12)
Index Cond: (sales_rep = 5495)
Filter: ((NOT deleted) AND ((hashed SubPlan 2) OR (sales_rep IS NULL)))
SubPlan 2
-> Sort (cost=10.31..10.32 rows=1 width=404)
Sort Key: tbl_users.pinyin1 NULLS FIRST, tbl_users.namecut1 NULLS FIRST, tbl_users.pinyin2 NULLS FIRST, tbl_users.namecut2 NULLS FIRST, tbl_users.pinyin3 NULLS FIRST, tbl_users.namecut3 NULLS FIRST, tbl_users.pinyin4 NULLS FIRST, tbl_users.namecut4 NULLS FIRST
-> Seq Scan on tbl_users (cost=0.00..10.30 rows=1 width=404)
Filter: ((NOT client_lock) AND (id = ANY ('{5495,5493}'::integer[])) AND (user_status = '1'::type_global_user_status))
-> Index Only Scan using tbl_client_classes_pkey on tbl_client_classes cl (cost=0.14..8.16 rows=1 width=4)
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=4)
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=34.01..34.01 rows=10 width=4)
-> Bitmap Heap Scan on tbl_sales_group_relationships sgr (cost=23.45..34.01 rows=10 width=4)
Recheck Cond: (sales_id = 5495)
-> Bitmap Index Scan on tbl_sales_group_relationships_pkey (cost=0.00..23.45 rows=10 width=0)
Index Cond: (sales_id = 5495)
-> 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)