QUERY PLAN
Sort (cost=493.32..493.33 rows=6 width=198)
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=480.49..493.24 rows=6 width=198)
Hash Cond: (ccr.schedule_id = s.id)
Filter: (((s.is_public = 1) AND (s.created_by <> 5250)) OR (s.created_by = 5250) OR (ccr.user_id = 5250))
-> Seq Scan on tbl_schedule_carbon_copy_rels ccr (cost=0.00..10.91 rows=691 width=8)
-> Hash (cost=480.41..480.41 rows=6 width=182)
-> Bitmap Heap Scan on tbl_schedules s (cost=75.95..480.41 rows=6 width=182)
Recheck Cond: (client_id = ANY ('{2166,3367,3370,2736,402,1663,3357,24,3134,571,520,2182,2818,3179,2202,3115,3262,2806,2173,3328}'::integer[]))
Filter: ((NOT is_complete) OR (complete_at >= '2025-09-02 00:00:00+08'::timestamp with time zone))
-> Bitmap Index Scan on tbl_schedules_client_id_idx (cost=0.00..75.95 rows=296 width=0)
Index Cond: (client_id = ANY ('{2166,3367,3370,2736,402,1663,3357,24,3134,571,520,2182,2818,3179,2202,3115,3262,2806,2173,3328}'::integer[]))