What does this MR do?
Fixes terrible performance that leads to SQL timeout: https://gitlab.com/gitlab-com/infrastructure/issues/3260.
The current query has multiple conditions and requires extensive DB access. The new outside/inside approach does indexed outside query to get IDs, and expensive inside query to perform filtering.
This code is already covered by tests of that worker.
Does this MR meet the acceptance criteria?
- Changelog entry added, if necessary
- Has been reviewed by Database
- Conform by the merge request performance guides
- Conform by the style guides
- Squashed related commits together