Files
openproject/spec/requests
Oliver Günther 7ae5604869 Merge pull request #23070 from opf/fix/relation-visible-scope
The performance decreased because of a combination of calls that were supposed to increase performance. 

We have a mechanism in place which automatically eager loads models needed in the element representers when a collection of them is rendered. This is to avoid N+1 queries of course. But, if eager loading is  combined with e.g., a LIMIT, which we do because we paginate, rails automatically falls back to issuing two instead of just one SQL statement. Which makes sense as otherwise LEFT JOINS might mess with the result set. 

But Rails does so in a somewhat simple fashion. It uses the first query to get the DISTINCT ids. The second is used to load the values (without a limit). But instead of removing all WHERE statements in the second SQL statement and then apply just the one for the ids, it keeps the original WHERE statement and applies the one for the ids on top. The problem with that is that the database trips on that (I didn't check the why) and uses a less than optimal query plan. 

That was the problem here as well. The first query remained reasonable quick (300ms) but the second one took 25s. 

The fix is to split the two statements by hand in the representer whenever eager loading is defined. The first query has all the filters but no eager loading and fetches the ids. The second takes the ids, and with eager loading included loads the data. Et voila, second query takes 10ms.   

That at least works for relations, work_packages and projects. But there are other representers that also seem to have custom behaviour. I'm looking into whether they can be easily adapted.
2026-05-07 15:44:13 +02:00
..
2025-05-16 14:56:25 +02:00
2026-03-23 13:29:57 +01:00
2026-02-09 15:07:55 +01:00
2025-09-24 13:46:09 +02:00
2025-12-09 11:21:56 +01:00
2025-05-05 09:29:55 +02:00