QUERY PLAN
Aggregate (cost=120.77..120.78 rows=1 width=8)
-> Sort (cost=120.75..120.76 rows=1 width=495)
Sort Key: (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
-> Group (cost=120.73..120.74 rows=1 width=495)
Group Key: s.id
-> Sort (cost=120.73..120.74 rows=1 width=21)
Sort Key: s.id
-> Hash Right Join (cost=108.58..120.72 rows=1 width=21)
Hash Cond: (ccr.schedule_id = s.id)
-> Seq Scan on tbl_schedule_carbon_copy_rels ccr (cost=0.00..10.45 rows=645 width=4)
-> Hash (cost=108.57..108.57 rows=1 width=29)
-> Bitmap Heap Scan on tbl_schedules s (cost=4.54..108.57 rows=1 width=29)
Recheck Cond: (created_by = 5250)
Filter: (NOT is_complete)
-> Bitmap Index Scan on tbl_schedules_created_by_idx (cost=0.00..4.54 rows=34 width=0)
Index Cond: (created_by = 5250)