QUERY PLAN
Limit (cost=712.25..712.28 rows=10 width=540)
-> Sort (cost=712.25..715.41 rows=1263 width=540)
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.created_at DESC
-> HashAggregate (cost=666.01..684.96 rows=1263 width=540)
Group Key: s.id
-> Hash Left Join (cost=27.06..662.86 rows=1263 width=520)
Hash Cond: (s.id = ccr.schedule_id)
Filter: (((s.created_by = ANY ('{5300,5295,5295}'::integer[])) OR (ccr.user_id = 5295)) AND (((s.is_public = 1) AND (s.created_by <> 5295)) OR (s.created_by = 5295) OR (ccr.user_id = 5295)))
-> Hash Join (cost=8.55..593.98 rows=6789 width=520)
Hash Cond: (s.created_by = u.id)
-> Seq Scan on tbl_schedules s (cost=0.00..546.17 rows=14517 width=520)
-> Hash (cost=7.74..7.74 rows=65 width=4)
-> Seq Scan on tbl_users u (cost=0.00..7.74 rows=65 width=4)
Filter: (department_id = 1)
-> Hash (cost=10.45..10.45 rows=645 width=8)
-> Seq Scan on tbl_schedule_carbon_copy_rels ccr (cost=0.00..10.45 rows=645 width=8)