QUERY PLAN
Limit (cost=535.22..535.24 rows=10 width=540)
-> Sort (cost=535.22..537.83 rows=1045 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=496.96..512.63 rows=1045 width=540)
Group Key: s.id
-> Nested Loop (cost=45.41..494.35 rows=1045 width=520)
-> Seq Scan on tbl_users u (cost=0.00..8.09 rows=1 width=4)
Filter: ((id = 1004) AND (department_id = 1))
-> Hash Left Join (cost=45.41..475.81 rows=1045 width=520)
Hash Cond: (s.id = ccr.schedule_id)
Filter: ((((s.is_public = 1) AND (s.created_by <> 1004)) OR (s.created_by = 1004) OR (ccr.user_id = 1004)) AND ((s.created_by = ANY ('{1004,1004,1001,5446,5087,1015,1023,5064,5440,5360,1022,5311,5248,1000,5420,5445,5388,5404,5252,1013,5300,5250,5296,1005,1017,5085,5332,1011,5329,5016,5295}'::integer[])) OR (ccr.user_id = 1004)))
-> Bitmap Heap Scan on tbl_schedules s (cost=26.89..445.01 rows=1369 width=520)
Recheck Cond: (created_by = 1004)
-> Bitmap Index Scan on tbl_schedules_created_by_idx (cost=0.00..26.55 rows=1369 width=0)
Index Cond: (created_by = 1004)
-> 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)