QUERY PLAN
Append (cost=1962.04..2509.72 rows=3 width=709)
-> Subquery Scan on "*SELECT* 1" (cost=1962.04..1962.10 rows=1 width=709)
-> Unique (cost=1962.04..1962.09 rows=1 width=749)
-> Sort (cost=1962.04..1962.04 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=1961.99..1962.03 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=1961.99..1962.00 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..1961.98 rows=1 width=748)
Join Filter: (meta.user_id = u.id)
-> Nested Loop Left Join (cost=1.13..1957.46 rows=1 width=677)
Join Filter: (c.sales_rep = sales.id)
-> Nested Loop Left Join (cost=1.13..1939.22 rows=1 width=668)
Join Filter: (ct.id = c.client_type)
-> Nested Loop Left Join (cost=1.13..1938.08 rows=1 width=522)
Join Filter: (cc.id = c.client_class)
-> Nested Loop Left Join (cost=1.13..1936.99 rows=1 width=376)
-> Nested Loop Left Join (cost=0.85..1936.32 rows=1 width=372)
-> Nested Loop Left Join (cost=0.57..1928.02 rows=1 width=315)
Join Filter: (u.department_id = d.id)
-> Nested Loop Left Join (cost=0.57..1926.82 rows=1 width=169)
Join Filter: (a.created_by = u.id)
-> Nested Loop Left Join (cost=0.57..1908.58 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..1900.27 rows=1 width=136)
-> Seq Scan on tbl_work_logs a (cost=0.00..1891.97 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 >= '2026-05-30 00:00:00+08'::timestamp with time zone) AND (start_at <= '2026-05-30 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..16.44 rows=144 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..16.44 rows=144 width=13)
-> Seq Scan on tbl_user_meta meta (cost=0.00..3.26 rows=101 width=75)
Filter: ((meta_key)::text = 'avatar'::text)
-> Subquery Scan on "*SELECT* 2" (cost=134.26..134.33 rows=1 width=709)
-> Unique (cost=134.26..134.32 rows=1 width=749)
-> Sort (cost=134.26..134.27 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=134.22..134.25 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=134.22..134.22 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.57..134.21 rows=1 width=748)
-> Nested Loop Left Join (cost=1.42..133.93 rows=1 width=677)
-> Nested Loop Left Join (cost=1.28..133.74 rows=1 width=668)
Join Filter: (ct_1.id = c_1.client_type)
-> Nested Loop Left Join (cost=1.28..132.60 rows=1 width=522)
Join Filter: (cc_1.id = c_1.client_class)
-> Nested Loop Left Join (cost=1.28..131.51 rows=1 width=376)
-> Nested Loop Left Join (cost=1.00..130.84 rows=1 width=372)
-> Nested Loop Left Join (cost=0.72..122.54 rows=1 width=315)
Join Filter: (u_1.department_id = d_1.id)
-> Nested Loop Left Join (cost=0.72..121.33 rows=1 width=169)
-> Nested Loop Left Join (cost=0.57..113.06 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..104.76 rows=1 width=136)
-> Seq Scan on tbl_pre_sales a_1 (cost=0.00..96.45 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 >= '2026-05-30 00:00:00+08'::timestamp with time zone) AND (start_at <= '2026-05-30 23:59:59+08'::timestamp with time zone))
-> 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)
-> 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)
-> Seq Scan on tbl_departments d_1 (cost=0.00..1.09 rows=9 width=150)
-> 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)
-> 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.20 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.27 rows=1 width=75)
Index Cond: ((user_id = u_1.id) AND ((meta_key)::text = 'avatar'::text))
-> Subquery Scan on "*SELECT* 3" (cost=413.23..413.30 rows=1 width=709)
-> Unique (cost=413.23..413.29 rows=1 width=749)
-> Sort (cost=413.23..413.24 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=413.19..413.22 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=413.19..413.19 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.57..413.18 rows=1 width=748)
-> Nested Loop Left Join (cost=1.42..412.90 rows=1 width=677)
-> Nested Loop Left Join (cost=1.28..412.71 rows=1 width=668)
Join Filter: (ct_2.id = c_2.client_type)
-> Nested Loop Left Join (cost=1.28..411.57 rows=1 width=522)
Join Filter: (cc_2.id = c_2.client_class)
-> Nested Loop Left Join (cost=1.28..410.48 rows=1 width=376)
-> Nested Loop Left Join (cost=1.00..409.81 rows=1 width=372)
-> Nested Loop Left Join (cost=0.72..401.51 rows=1 width=315)
Join Filter: (u_2.department_id = d_2.id)
-> Nested Loop Left Join (cost=0.72..400.30 rows=1 width=169)
-> Nested Loop Left Join (cost=0.57..392.03 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..383.73 rows=1 width=136)
-> Seq Scan on tbl_implement_records a_2 (cost=0.00..375.42 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 >= '2026-05-30 00:00:00+08'::timestamp with time zone) AND (arrived_at <= '2026-05-30 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)
-> Index Scan using tbl_users_pkey on tbl_users u_2 (cost=0.14..8.16 rows=1 width=17)
Index Cond: (a_2.created_by = id)
-> 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.20 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.27 rows=1 width=75)
Index Cond: ((user_id = u_2.id) AND ((meta_key)::text = 'avatar'::text))