QUERY PLAN
Append (cost=1830.45..2320.54 rows=3 width=709)
-> Subquery Scan on "*SELECT* 1" (cost=1830.45..1830.51 rows=1 width=709)
-> Unique (cost=1830.45..1830.50 rows=1 width=749)
-> Sort (cost=1830.45..1830.45 rows=1 width=749)
Sort Key: a.client_id, ((a.end_at - a.start_at)), a.created_by, a.start_at, a.end_at, a.created_at, a.location_id, (CASE WHEN (a.location_in_id IS NOT NULL) THEN lin.geolocation[0] ELSE lout.geolocation[0] END), (CASE WHEN (a.location_in_id IS NOT NULL) THEN lin.geolocation[1] ELSE lout.geolocation[1] END), (CASE WHEN (p.id IS NULL) THEN false ELSE true END), c.name, ct.name, cc.name, sales.name, lin.address, a.indistance, d.name, u.name, meta.meta_value
-> Group (cost=1830.40..1830.44 rows=1 width=749)
Group Key: a.id, lin.id, lout.id, p.id, ct.id, sales.id, cc.id, d.id, u.id, c.id, meta.meta_value
-> Sort (cost=1830.40..1830.41 rows=1 width=748)
Sort Key: a.id, lin.id, lout.id, p.id, ct.id, sales.id, cc.id, d.id, u.id, c.id, meta.meta_value
-> Nested Loop Left Join (cost=1.13..1830.39 rows=1 width=748)
Join Filter: (meta.user_id = u.id)
-> Nested Loop Left Join (cost=1.13..1825.94 rows=1 width=677)
Join Filter: (c.sales_rep = sales.id)
-> Nested Loop Left Join (cost=1.13..1816.81 rows=1 width=668)
Join Filter: (ct.id = c.client_type)
-> Nested Loop Left Join (cost=1.13..1815.68 rows=1 width=522)
Join Filter: (cc.id = c.client_class)
-> Nested Loop Left Join (cost=1.13..1814.59 rows=1 width=376)
-> Nested Loop Left Join (cost=0.85..1813.92 rows=1 width=372)
-> Nested Loop Left Join (cost=0.57..1805.62 rows=1 width=315)
Join Filter: (u.department_id = d.id)
-> Nested Loop Left Join (cost=0.57..1804.41 rows=1 width=169)
Join Filter: (a.created_by = u.id)
-> Nested Loop Left Join (cost=0.57..1795.29 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..1786.98 rows=1 width=136)
-> Seq Scan on tbl_work_logs a (cost=0.00..1778.67 rows=1 width=56)
Filter: ((client_id IS NOT NULL) AND ((location_in_id IS NOT NULL) OR (location_out_id IS NOT NULL)) AND (location_in_id IS NOT NULL) AND (location_out_id IS NULL) AND (start_at >= '2025-07-07 00:00:00+08'::timestamp with time zone) AND (start_at <= '2025-07-07 23:59:59+08'::timestamp with time zone))
-> Index Scan using tbl_location_pkey on tbl_location lin (cost=0.29..8.30 rows=1 width=80)
Index Cond: (a.location_in_id = id)
-> Index Scan using tbl_location_pkey on tbl_location lout (cost=0.29..8.30 rows=1 width=20)
Index Cond: (a.location_out_id = id)
-> Seq Scan on tbl_users u (cost=0.00..7.39 rows=139 width=17)
-> Seq Scan on tbl_departments d (cost=0.00..1.09 rows=9 width=150)
-> Index Scan using tbl_clients_pkey on tbl_clients c (cost=0.28..8.29 rows=1 width=57)
Index Cond: (id = a.client_id)
-> Index Scan using tbl_projects_client_id_idx on tbl_projects p (cost=0.28..0.65 rows=2 width=8)
Index Cond: (client_id = c.id)
Filter: (project_status = 3)
-> Seq Scan on tbl_client_classes cc (cost=0.00..1.04 rows=4 width=150)
-> Seq Scan on tbl_client_types ct (cost=0.00..1.06 rows=6 width=150)
-> Seq Scan on tbl_users sales (cost=0.00..7.39 rows=139 width=13)
-> Seq Scan on tbl_user_meta meta (cost=0.00..3.23 rows=98 width=75)
Filter: ((meta_key)::text = 'avatar'::text)
-> Subquery Scan on "*SELECT* 2" (cost=127.59..127.65 rows=1 width=709)
-> Unique (cost=127.59..127.64 rows=1 width=749)
-> Sort (cost=127.59..127.59 rows=1 width=749)
Sort Key: a_1.client_id, ((a_1.end_at - a_1.start_at)), a_1.created_by, a_1.start_at, a_1.end_at, a_1.created_at, a_1.location_id, (CASE WHEN (a_1.location_in_id IS NOT NULL) THEN lin_1.geolocation[0] ELSE lout_1.geolocation[0] END), (CASE WHEN (a_1.location_in_id IS NOT NULL) THEN lin_1.geolocation[1] ELSE lout_1.geolocation[1] END), (CASE WHEN (p_1.id IS NULL) THEN false ELSE true END), c_1.name, ct_1.name, cc_1.name, sales_1.name, lin_1.address, a_1.indistance, d_1.name, u_1.name, meta_1.meta_value
-> Group (cost=127.54..127.58 rows=1 width=749)
Group Key: a_1.id, lin_1.id, lout_1.id, p_1.id, ct_1.id, sales_1.id, cc_1.id, d_1.id, u_1.id, c_1.id, meta_1.meta_value
-> Sort (cost=127.54..127.55 rows=1 width=748)
Sort Key: a_1.id, lin_1.id, lout_1.id, p_1.id, ct_1.id, sales_1.id, cc_1.id, d_1.id, u_1.id, c_1.id, meta_1.meta_value
-> Nested Loop Left Join (cost=1.70..127.53 rows=1 width=748)
-> Nested Loop Left Join (cost=1.56..127.25 rows=1 width=677)
-> Nested Loop Left Join (cost=1.41..127.07 rows=1 width=668)
Join Filter: (ct_1.id = c_1.client_type)
-> Nested Loop Left Join (cost=1.41..125.94 rows=1 width=522)
Join Filter: (cc_1.id = c_1.client_class)
-> Nested Loop Left Join (cost=1.41..124.85 rows=1 width=376)
-> Nested Loop Left Join (cost=1.12..116.54 rows=1 width=360)
-> Nested Loop Left Join (cost=0.84..108.23 rows=1 width=280)
-> Nested Loop Left Join (cost=0.56..99.80 rows=1 width=117)
-> Nested Loop Left Join (cost=0.28..99.13 rows=1 width=113)
-> Seq Scan on tbl_pre_sales a_1 (cost=0.00..90.83 rows=1 width=56)
Filter: ((client_id IS NOT NULL) AND ((location_in_id IS NOT NULL) OR (location_out_id IS NOT NULL)) AND (location_in_id IS NOT NULL) AND (location_out_id IS NULL) AND (start_at >= '2025-07-07 00:00:00+08'::timestamp with time zone) AND (start_at <= '2025-07-07 23:59:59+08'::timestamp with time zone))
-> Index Scan using tbl_clients_pkey on tbl_clients c_1 (cost=0.28..8.29 rows=1 width=57)
Index Cond: (id = a_1.client_id)
-> Index Scan using tbl_projects_client_id_idx on tbl_projects p_1 (cost=0.28..0.65 rows=2 width=8)
Index Cond: (client_id = c_1.id)
Filter: (project_status = 3)
-> Nested Loop Left Join (cost=0.28..8.42 rows=1 width=163)
-> Index Scan using tbl_users_pkey on tbl_users u_1 (cost=0.14..8.16 rows=1 width=17)
Index Cond: (a_1.created_by = id)
-> Index Scan using tbl_departments_pkey on tbl_departments d_1 (cost=0.14..0.24 rows=1 width=150)
Index Cond: (u_1.department_id = id)
-> Index Scan using tbl_location_pkey on tbl_location lin_1 (cost=0.29..8.30 rows=1 width=80)
Index Cond: (a_1.location_in_id = id)
-> Index Scan using tbl_location_pkey on tbl_location lout_1 (cost=0.29..8.30 rows=1 width=20)
Index Cond: (a_1.location_out_id = id)
-> Seq Scan on tbl_client_classes cc_1 (cost=0.00..1.04 rows=4 width=150)
-> Seq Scan on tbl_client_types ct_1 (cost=0.00..1.06 rows=6 width=150)
-> Index Scan using tbl_users_pkey on tbl_users sales_1 (cost=0.14..0.18 rows=1 width=13)
Index Cond: (c_1.sales_rep = id)
-> Index Scan using tbl_user_meta_pkey on tbl_user_meta meta_1 (cost=0.14..0.28 rows=1 width=75)
Index Cond: ((user_id = u_1.id) AND ((meta_key)::text = 'avatar'::text))
-> Subquery Scan on "*SELECT* 3" (cost=362.32..362.39 rows=1 width=709)
-> Unique (cost=362.32..362.38 rows=1 width=749)
-> Sort (cost=362.32..362.33 rows=1 width=749)
Sort Key: a_2.client_id, ((a_2.left_at - a_2.arrived_at)), a_2.created_by, a_2.arrived_at, a_2.left_at, a_2.created_at, a_2.location_id, (CASE WHEN (a_2.location_in_id IS NOT NULL) THEN lin_2.geolocation[0] ELSE lout_2.geolocation[0] END), (CASE WHEN (a_2.location_in_id IS NOT NULL) THEN lin_2.geolocation[1] ELSE lout_2.geolocation[1] END), (CASE WHEN (p_2.id IS NULL) THEN false ELSE true END), c_2.name, ct_2.name, cc_2.name, sales_2.name, lin_2.address, a_2.indistance, d_2.name, u_2.name, meta_2.meta_value
-> Group (cost=362.28..362.31 rows=1 width=749)
Group Key: a_2.id, lin_2.id, lout_2.id, p_2.id, ct_2.id, sales_2.id, cc_2.id, d_2.id, u_2.id, c_2.id, meta_2.meta_value
-> Sort (cost=362.28..362.28 rows=1 width=748)
Sort Key: a_2.id, lin_2.id, lout_2.id, p_2.id, ct_2.id, sales_2.id, cc_2.id, d_2.id, u_2.id, c_2.id, meta_2.meta_value
-> Nested Loop Left Join (cost=1.42..362.27 rows=1 width=748)
-> Nested Loop Left Join (cost=1.28..361.99 rows=1 width=677)
-> Nested Loop Left Join (cost=1.13..361.81 rows=1 width=668)
Join Filter: (ct_2.id = c_2.client_type)
-> Nested Loop Left Join (cost=1.13..360.67 rows=1 width=522)
Join Filter: (cc_2.id = c_2.client_class)
-> Nested Loop Left Join (cost=1.13..359.58 rows=1 width=376)
-> Nested Loop Left Join (cost=0.85..358.92 rows=1 width=372)
-> Nested Loop Left Join (cost=0.57..350.61 rows=1 width=315)
Join Filter: (u_2.department_id = d_2.id)
-> Nested Loop Left Join (cost=0.57..349.41 rows=1 width=169)
Join Filter: (a_2.created_by = u_2.id)
-> Nested Loop Left Join (cost=0.57..340.28 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..331.98 rows=1 width=136)
-> Seq Scan on tbl_implement_records a_2 (cost=0.00..323.67 rows=1 width=56)
Filter: ((client_id IS NOT NULL) AND ((location_in_id IS NOT NULL) OR (location_out_id IS NOT NULL)) AND (location_in_id IS NOT NULL) AND (location_out_id IS NULL) AND (arrived_at >= '2025-07-07 00:00:00+08'::timestamp with time zone) AND (arrived_at <= '2025-07-07 23:59:59+08'::timestamp with time zone))
-> Index Scan using tbl_location_pkey on tbl_location lin_2 (cost=0.29..8.30 rows=1 width=80)
Index Cond: (a_2.location_in_id = id)
-> Index Scan using tbl_location_pkey on tbl_location lout_2 (cost=0.29..8.30 rows=1 width=20)
Index Cond: (a_2.location_out_id = id)
-> Seq Scan on tbl_users u_2 (cost=0.00..7.39 rows=139 width=17)
-> Seq Scan on tbl_departments d_2 (cost=0.00..1.09 rows=9 width=150)
-> Index Scan using tbl_clients_pkey on tbl_clients c_2 (cost=0.28..8.29 rows=1 width=57)
Index Cond: (id = a_2.client_id)
-> Index Scan using tbl_projects_client_id_idx on tbl_projects p_2 (cost=0.28..0.65 rows=2 width=8)
Index Cond: (client_id = c_2.id)
Filter: (project_status = 3)
-> Seq Scan on tbl_client_classes cc_2 (cost=0.00..1.04 rows=4 width=150)
-> Seq Scan on tbl_client_types ct_2 (cost=0.00..1.06 rows=6 width=150)
-> Index Scan using tbl_users_pkey on tbl_users sales_2 (cost=0.14..0.18 rows=1 width=13)
Index Cond: (c_2.sales_rep = id)
-> Index Scan using tbl_user_meta_pkey on tbl_user_meta meta_2 (cost=0.14..0.28 rows=1 width=75)
Index Cond: ((user_id = u_2.id) AND ((meta_key)::text = 'avatar'::text))