QUERY PLAN
Append (cost=335.33..850.22 rows=3 width=709)
-> Subquery Scan on "*SELECT* 1" (cost=335.33..335.39 rows=1 width=709)
-> Unique (cost=335.33..335.38 rows=1 width=749)
-> Sort (cost=335.33..335.34 rows=1 width=749)
Sort Key: a.client_id, ((a.end_at - a.start_at)), 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=335.29..335.32 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=335.29..335.29 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=6.27..335.28 rows=1 width=748)
Join Filter: (meta.user_id = u.id)
-> Nested Loop Left Join (cost=6.27..331.79 rows=1 width=677)
-> Nested Loop Left Join (cost=6.12..331.61 rows=1 width=668)
Join Filter: (ct.id = c.client_type)
-> Nested Loop Left Join (cost=6.12..330.48 rows=1 width=522)
Join Filter: (cc.id = c.client_class)
-> Nested Loop Left Join (cost=6.12..329.39 rows=1 width=376)
-> Nested Loop Left Join (cost=5.84..328.72 rows=1 width=372)
-> Nested Loop Left Join (cost=5.56..320.42 rows=1 width=315)
Join Filter: (u.department_id = d.id)
-> Nested Loop Left Join (cost=5.56..319.21 rows=1 width=169)
Join Filter: (a.created_by = u.id)
-> Nested Loop Left Join (cost=5.56..311.46 rows=1 width=152)
-> Nested Loop Left Join (cost=5.28..303.16 rows=1 width=136)
-> Bitmap Heap Scan on tbl_work_logs a (cost=4.99..294.85 rows=1 width=56)
Recheck Cond: (created_by = 1000)
Filter: ((client_id IS NOT NULL) AND ((location_in_id IS NOT NULL) OR (location_out_id IS NOT NULL)) AND (start_at >= '2025-07-01 00:00:00+08'::timestamp with time zone) AND (start_at <= '2025-07-31 23:59:59+08'::timestamp with time zone))
-> Bitmap Index Scan on tbl_work_logs_created_by_idx (cost=0.00..4.99 rows=93 width=0)
Index Cond: (created_by = 1000)
-> 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.74 rows=1 width=17)
Filter: (id = 1000)
-> 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)
-> Index Scan using tbl_users_pkey on tbl_users sales (cost=0.14..0.18 rows=1 width=13)
Index Cond: (c.sales_rep = id)
-> Seq Scan on tbl_user_meta meta (cost=0.00..3.47 rows=1 width=75)
Filter: ((user_id = 1000) AND ((meta_key)::text = 'avatar'::text))
-> Subquery Scan on "*SELECT* 2" (cost=135.11..135.17 rows=1 width=709)
-> Unique (cost=135.11..135.16 rows=1 width=749)
-> Sort (cost=135.11..135.12 rows=1 width=749)
Sort Key: a_1.client_id, ((a_1.end_at - a_1.start_at)), 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=135.07..135.10 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=135.07..135.07 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.28..135.06 rows=1 width=748)
Join Filter: (meta_1.user_id = u_1.id)
-> Nested Loop Left Join (cost=1.28..131.58 rows=1 width=677)
-> Nested Loop Left Join (cost=1.13..131.40 rows=1 width=668)
Join Filter: (ct_1.id = c_1.client_type)
-> Nested Loop Left Join (cost=1.13..130.26 rows=1 width=522)
Join Filter: (cc_1.id = c_1.client_class)
-> Nested Loop Left Join (cost=1.13..129.17 rows=1 width=376)
-> Nested Loop Left Join (cost=0.85..128.50 rows=1 width=372)
-> Nested Loop Left Join (cost=0.57..120.20 rows=1 width=315)
Join Filter: (u_1.department_id = d_1.id)
-> Nested Loop Left Join (cost=0.57..119.00 rows=1 width=169)
Join Filter: (a_1.created_by = u_1.id)
-> Nested Loop Left Join (cost=0.57..111.25 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..102.94 rows=1 width=136)
-> Seq Scan on tbl_pre_sales a_1 (cost=0.00..94.64 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 (start_at >= '2025-07-01 00:00:00+08'::timestamp with time zone) AND (start_at <= '2025-07-31 23:59:59+08'::timestamp with time zone) AND (created_by = 1000))
-> 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_users u_1 (cost=0.00..7.74 rows=1 width=17)
Filter: (id = 1000)
-> 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.18 rows=1 width=13)
Index Cond: (c_1.sales_rep = id)
-> Seq Scan on tbl_user_meta meta_1 (cost=0.00..3.47 rows=1 width=75)
Filter: ((user_id = 1000) AND ((meta_key)::text = 'avatar'::text))
-> Subquery Scan on "*SELECT* 3" (cost=379.59..379.65 rows=1 width=709)
-> Unique (cost=379.59..379.64 rows=1 width=749)
-> Sort (cost=379.59..379.60 rows=1 width=749)
Sort Key: a_2.client_id, ((a_2.left_at - a_2.arrived_at)), 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=379.55..379.58 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=379.55..379.55 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.28..379.54 rows=1 width=748)
Join Filter: (meta_2.user_id = u_2.id)
-> Nested Loop Left Join (cost=1.28..376.06 rows=1 width=677)
-> Nested Loop Left Join (cost=1.13..375.88 rows=1 width=668)
Join Filter: (ct_2.id = c_2.client_type)
-> Nested Loop Left Join (cost=1.13..374.74 rows=1 width=522)
Join Filter: (cc_2.id = c_2.client_class)
-> Nested Loop Left Join (cost=1.13..373.65 rows=1 width=376)
-> Nested Loop Left Join (cost=0.85..372.98 rows=1 width=372)
-> Nested Loop Left Join (cost=0.57..364.68 rows=1 width=315)
Join Filter: (u_2.department_id = d_2.id)
-> Nested Loop Left Join (cost=0.57..363.48 rows=1 width=169)
Join Filter: (a_2.created_by = u_2.id)
-> Nested Loop Left Join (cost=0.57..355.73 rows=1 width=152)
-> Nested Loop Left Join (cost=0.29..347.42 rows=1 width=136)
-> Seq Scan on tbl_implement_records a_2 (cost=0.00..339.12 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 (arrived_at >= '2025-07-01 00:00:00+08'::timestamp with time zone) AND (arrived_at <= '2025-07-31 23:59:59+08'::timestamp with time zone) AND (created_by = 1000))
-> 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.74 rows=1 width=17)
Filter: (id = 1000)
-> 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)
-> Seq Scan on tbl_user_meta meta_2 (cost=0.00..3.47 rows=1 width=75)
Filter: ((user_id = 1000) AND ((meta_key)::text = 'avatar'::text))