Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N+1 Queries #72

Closed
rafaucau opened this issue Jul 2, 2024 · 7 comments · Fixed by #73
Closed

N+1 Queries #72

rafaucau opened this issue Jul 2, 2024 · 7 comments · Fixed by #73
Labels
bug Something isn't working

Comments

@rafaucau
Copy link
Contributor

rafaucau commented Jul 2, 2024

Bug Report

Current Behavior
When the extension is enabled, there are a lot of queries to the database when loading the discussion list.
This line is responsible for this:

$state = $tag->stateFor($serializer->getActor());

Steps to Reproduce

  1. Enable Clockwork
  2. Load the Discussion list, e.g., Homepage
  3. Observe the number of database queries executed

Expected Behavior
The subscription state should be eagerly loaded to minimize the number of database queries.

Screenshots

image

Environment

  • Flarum version: 1.8.5
  • Extension version: 1.2.2

Additional Context
This issue was addressed in #45 but reverted in #46.

@rafaucau rafaucau added the bug Something isn't working label Jul 2, 2024
@dsevillamartin
Copy link
Member

dsevillamartin commented Jul 6, 2024

That reversion seems simply due to the state for a tag not existing... which is a valid scenario if the user hasn't configured anything about the tags that use the user state. So not 100% sure why it was just reverted instead of adding a null check.

Change goes from
image
to
image

Released v1.2.3 with this. Thanks!

@rafaucau
Copy link
Contributor Author

rafaucau commented Jul 11, 2024

I'm now experiencing an issue with the subscription state on my forum with v1.2.3. On some tag pages, the subscription state is missing, but only on certain tags. Additionally, changing the follow status for these tags has no effect; the API returns null regardless of my selection:

image
image

However, the tag_user table reflects the changes, while the tag page always shows the status as not followed (null/default).

I cannot reproduce this issue on a clean installation of Flarum. I checked on my forum with all extensions disabled except for Tags and Follow Tags, and the problem still persists. It might be related to the number of tags, above which the status does not load. I have 2544 tags in my database.

I have tested everything and noticed that the API request query params includes state. This seems like a random issue. Additionally, when I use the My Tags extension, I can see the list of followed tags changing as I navigate between tag pages, as if the relationships in the discussion list sometimes includes the subscription state for certain tags and sometimes do not.

Do you have any suggestions on what could be causing this issue?

@rafaucau
Copy link
Contributor Author

rafaucau commented Jul 11, 2024

I have found the cause: flarum/framework#4007.
The problem occurs if you are not the first user who has data in the tag_user table.
Follow Tags is currently broken. It is better to rollback the changes from v1.2.3 until the problem is fixed in Flarum. @dsevillamartin

@dsevillamartin
Copy link
Member

This is interesting. I attempted a fix - can you see if #73 fixes all those instances for you? It should still improve performance over v1.2.3 (though not as much)

@rafaucau
Copy link
Contributor Author

rafaucau commented Jul 12, 2024

This solves the problem, but the N+1 queries came back. For best performance, it would be worth fixing this problem directly in Flarum

Before (v1.2.3) After (#73)
image image

@dsevillamartin
Copy link
Member

I agree, but once it's fixed in Flarum it should be fine in follow tags. The queries made in that PR are less if the actor was the first to have a tag state created, I believe - or if they visit a tag directly I think. I'm not 100% sure of when the state passed is for the correct user, but I do believe at least it makes a few less queries sometimes.

@dsevillamartin
Copy link
Member

I pushed v1.2.4 with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants