QUERY PLAN
Unique (cost=496.59..519.53 rows=148 width=539)
-> Sort (cost=496.59..496.96 rows=148 width=539)
Sort Key: c.name, c.abbr, c.pinyin_shortcut, c.description, c.corp_rep, c.website, c.capital, c.established_at, c.headcount, c.reg_address, c.working_address, c.telephone, c.fax, c.scale, c.branch, c.purchase_scale_per_yr, c.purchase_freq, c.company_nature, c.client_class, c.client_industry, c.client_type, c.sales_rep, c.main_contact, c.created_by, c.created_at, c.updated_at, c.main_business, c.deleted, c.capital_currency, c.invoice_info, c.logo, c.en_name, c.pick_at, c.it_budget, c.deployment_mode, c.it_per_num, c.public_relations_difficulty, c.it_status, c.decision_process, c.coach, c.competitor, c.peers, c.supplier, c.main_product, c.client_base_info_count, c.client_key_info_count, c.contact_count, c.visit_count, c.cooperate_count, c.case_pre_sales_count, c.case_implement_count, c.is_overdue_remind, c.from_contact, c.tax_no, (CASE WHEN (c.sales_rep IS NULL) THEN 0 ELSE c.sales_rep END), (CASE WHEN (c.sales_rep IS NULL) THEN 0 ELSE 1 END), (CASE WHEN (p.id IS NULL) THEN false ELSE true END), sl_agg.lost_contact_days, ((date_part('epoch'::text, (now() - max(tbl_work_logs.end_at))) / '86400'::double precision)), sl_agg.last_contact, cc.notice_days
-> Nested Loop Left Join (cost=14.38..491.25 rows=148 width=539)
Join Filter: (sl_agg.client_id = c.id)
-> Nested Loop Left Join (cost=9.50..238.09 rows=2 width=518)
Join Filter: (tbl_work_logs.client_id = c.id)
-> Nested Loop Left Join (cost=4.57..21.02 rows=1 width=510)
Join Filter: (cc.id = c.client_class)
-> Nested Loop Left Join (cost=4.57..19.93 rows=1 width=508)
Join Filter: (p.client_id = c.id)
-> Index Scan using tbl_clients_pkey on tbl_clients c (cost=0.28..8.29 rows=1 width=504)
Index Cond: (id = 1188)
Filter: (NOT deleted)
-> Bitmap Heap Scan on tbl_projects p (cost=4.30..11.63 rows=1 width=8)
Recheck Cond: (client_id = 1188)
Filter: (project_status = 3)
-> Bitmap Index Scan on tbl_projects_client_id_idx (cost=0.00..4.29 rows=2 width=0)
Index Cond: (client_id = 1188)
-> Seq Scan on tbl_client_classes cc (cost=0.00..1.04 rows=4 width=6)
-> GroupAggregate (cost=4.93..217.03 rows=2 width=12)
Group Key: tbl_work_logs.client_id
-> Bitmap Heap Scan on tbl_work_logs (cost=4.93..216.98 rows=2 width=12)
Recheck Cond: ((client_id IS NOT NULL) AND (client_id = 1188))
Filter: ((NOT is_draft) AND (location_in_id IS NOT NULL) AND (location_out_id IS NOT NULL))
-> Bitmap Index Scan on tbl_work_logs_client_id_idx (cost=0.00..4.93 rows=64 width=0)
Index Cond: ((client_id IS NOT NULL) AND (client_id = 1188))
-> Materialize (cost=4.88..253.11 rows=74 width=20)
-> Subquery Scan on sl_agg (cost=4.88..252.74 rows=74 width=20)
-> GroupAggregate (cost=4.88..252.00 rows=74 width=20)
Group Key: tbl_work_logs_1.client_id
-> Bitmap Heap Scan on tbl_work_logs tbl_work_logs_1 (cost=4.88..249.95 rows=76 width=12)
Recheck Cond: (client_id = 1188)
-> Bitmap Index Scan on tbl_work_logs_client_id_idx (cost=0.00..4.86 rows=76 width=0)
Index Cond: (client_id = 1188)