QUERY PLAN
Aggregate (cost=59.32..59.33 rows=1 width=8)
-> Sort (cost=59.28..59.29 rows=2 width=228)
Sort Key: s.target_completed_at, s.updated_at DESC
-> Group (cost=59.26..59.27 rows=2 width=228)
Group Key: s.id, u.id
-> Sort (cost=59.26..59.27 rows=2 width=26)
Sort Key: s.id
-> Nested Loop (cost=0.14..59.25 rows=2 width=26)
-> Index Scan using tbl_users_pkey on tbl_users u (cost=0.14..8.16 rows=1 width=4)
Index Cond: (id = 1001)
Filter: (department_id = 4)
-> Seq Scan on tbl_tasks s (cost=0.00..51.06 rows=2 width=34)
Filter: ((parent_id IS NULL) AND (head_of = 1001) AND (status = '0'::smallint))