-
Notifications
You must be signed in to change notification settings - Fork 48
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
fix(api): mitigate slow queries #4521
Conversation
⛔ Feature branch deployment currently inactive.If the PR is still open, you can add the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a test, we don't yet have good API tests for reading lists of content nodes
The newly added Api Tests pass with and without the code change in this PR, I checked. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for investigating! Worked in my manual test on mobile. Did not look at the code though.
I found out a bit more specifically on this topic: There's indeed a configurable threshold named Generally, our queries have a lot of joins - for the queries involved for content nodes I counted up to 14 joins. This comes from the eager loading strategy + from the various filters applied. It could make sense to look into this to see if our optimal settings would be different. On the other hand, as long as we can easily optimize the queries from PHP side, I'd prefer not to tinker too much with the DB settings. Further reading: |
We have a bunch of very slow queries, mainly on the /content_nodes endpoint.
Normally, it shouldn't make much difference whether to use WHERE or ON for join statements and the DB would optimize them anyway. But seems, our queries are too complex and this optimization doesn't work.
Hope I caught the biggest offenders. Appreciate if we can test & deploy this as soon as possible.
Timing tests on my local machine (with prod data dump), before PR
Timing tests on my local machine (with pod data dump), after PR
How to check for slow queries
(documentation for the next time)
I followed this guide: https://www.crunchydata.com/blog/tentative-smarter-query-optimization-in-postgres-starts-with-pg_stat_statements
And then use the following query:
The 4 top entries are all from the /content_nodes endpoint and with average runtime between 8 and 40 seconds (!).
![image](https://private-user-images.githubusercontent.com/449555/298706549-aa371af3-6778-45ab-a15f-5c6e407ce368.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAyNTYyMjMsIm5iZiI6MTcyMDI1NTkyMywicGF0aCI6Ii80NDk1NTUvMjk4NzA2NTQ5LWFhMzcxYWYzLTY3NzgtNDVhYi1hMTVmLTVjNmU0MDdjZTM2OC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwNlQwODUyMDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT02ZDg4NzZjYzFkNmVjNGRhNzIyZWIxYjgxOGU4OTQ5NjY4YjAwYzk3ZDVjNTI3ZjIxNjgxZjQ0N2QzZTM5NWYzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.TB9Qoz912_bB4EjytwfSXSTJ_Tae10fdavnjXHx9f0Y)