QUERY PLAN
Sort (cost=373.71..373.71 rows=3 width=199)
Sort Key: s.set_top_at DESC NULLS LAST, (CASE WHEN s.is_complete THEN 3 WHEN ((NOT s.is_complete) AND (s.end_at <= now())) THEN 2 ELSE 1 END), (CASE WHEN s.is_complete THEN s.complete_at ELSE NULL::timestamp with time zone END) DESC, (CASE WHEN (NOT s.is_complete) THEN s.end_at ELSE NULL::timestamp with time zone END), s.end_at DESC
-> Hash Right Join (cost=361.52..373.68 rows=3 width=199)
Hash Cond: (ccr.schedule_id = s.id)
Filter: (((s.is_public = 1) AND (s.created_by <> 1001)) OR (s.created_by = 1001) OR (ccr.user_id = 1001))
-> Seq Scan on tbl_schedule_carbon_copy_rels ccr (cost=0.00..10.45 rows=645 width=8)
-> Hash (cost=361.49..361.49 rows=3 width=183)
-> Bitmap Heap Scan on tbl_schedules s (cost=74.75..361.49 rows=3 width=183)
Recheck Cond: (client_id = ANY ('{1661,146,982,2538,2616,1933,2205,3081,3334,3327,3311,3304,3301,3296,3286,3278,3009,2845,2838,2836}'::integer[]))
Filter: ((NOT is_complete) OR (complete_at >= '2025-07-10 00:00:00+08'::timestamp with time zone))
-> Bitmap Index Scan on tbl_schedules_client_id_idx (cost=0.00..74.75 rows=139 width=0)
Index Cond: (client_id = ANY ('{1661,146,982,2538,2616,1933,2205,3081,3334,3327,3311,3304,3301,3296,3286,3278,3009,2845,2838,2836}'::integer[]))