pycroft icon indicating copy to clipboard operation
pycroft copied to clipboard

N+1 Query in `user.member_request_edit`

Open agdsn-sentry[bot] opened this issue 1 year ago • 4 comments

This causes 41(!!) lazy fetches on every request, adding up to ~120ms.

Sentry Issue: PYCROFT-9D

Offending Spans db - SELECT building.site_id AS building_site_i...

agdsn-sentry[bot] avatar Sep 18 '24 08:09 agdsn-sentry[bot]

I'm really not sure why this happens. We have precisely 41 buildings, so my guess would be that it's the QuerySelectField. However, this basically only uses a Building.q.all() – so why would sqla lazy-load happen that fetches every building given the building id?

Needs investigation.

lukasjuhrich avatar Sep 18 '24 09:09 lukasjuhrich

relevant frontend endpoint: https://github.com/agdsn/pycroft/blob/968f2adde70f108cc88c3b9f5dc5b7c62f541ba2/web/blueprints/user/init.py#L1138-L1194

lukasjuhrich avatar Sep 18 '24 09:09 lukasjuhrich

It seems that the slow transactions are the POST ones, GET goes through rather quickly.

lukasjuhrich avatar Sep 18 '24 09:09 lukasjuhrich

I think validation might be the culprit, specifically https://github.com/wtforms/wtforms-sqlalchemy/blob/09b3d4745ec98d6d8f769f6794bc217c63d81946/wtforms_sqlalchemy/fields.py#L184-L193 (or the building_query it calls).

lukasjuhrich avatar Sep 18 '24 09:09 lukasjuhrich