perf: Try to improve some internal DB queries
Created by: eseliger
Disclaimer: This PR is meant as an experiment and I am not yet 1000% sure if the performance impact is as expected. Hence, I would like to ship this and see what happens and potentially adjust/revert later. I looked at a bunch of query plans and the improvements can be validated, but sometimes queries behave differently when run very often.
Explanatory comments and query plans are in inline comments. I could have split these changes up in separate PRs, but I think the overall impact can be measured best when we do these things at once, as they affect each other (they all reduce pressure on the repo table). My hope is that this will get down latencies and CPU load a bit, and hopefully make it so we consume a bunch less memory (which might allow for more things to be in cache, so better performance for other queries, too).