Optimize Ci::Pipeline.latest query
Since we already know which ref we want, we could filter it out first.
We could also add an index for ci_commits (gl_project_id, ref, status) which could further optimize the query, and it could also be used in some other places.
Closes #26570 (closed)