-
-
Notifications
You must be signed in to change notification settings - Fork 10k
Optimize homepage post loading by eliminating N+1 queries for user data #7644
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
Conversation
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
…t loading Co-authored-by: ruibaby <21301288+ruibaby@users.noreply.github.com>
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
1 similar comment
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
Superseded by #7668. /close |
@JohnNiang: Closed this PR. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
…ta (#7668) #### What type of PR is this? /kind improvement /area core /milestone 2.21.x #### What this PR does / why we need it: This PR refactors posts query to reduce database queries significantly. #### Which issue(s) this PR fixes: Fixes #7593 Supersedes #7644 #### Does this PR introduce a user-facing change? ```release-note 优化首页、归档页加载速度 ```
Problem
The homepage post loading was suffering from a classic N+1 query problem when fetching user data (post owners and contributors). For each post displayed on the homepage, the system was making individual database queries to fetch user information, resulting in:
This created a significant performance bottleneck, especially visible in the SQL logs as repeated
SELECT users.* FROM users WHERE users.ID = $1 LIMIT 2
queries.Solution
Implemented batch fetching to reduce the query complexity from O(N) to O(1) for user data:
1. Added batch user fetching capability
This method uses a single query with
IN
clause to fetch multiple users at once, falling back to ghost users for missing entries.2. Modified post processing to use batching
3. Cache-aware post conversion
Added overloaded methods that accept pre-fetched user data:
Performance Impact
Before: 1 + N + M database queries
After: 2 database queries (1 for posts, 1 for all users)
For a homepage with 10 posts having 2 contributors each, this reduces queries from ~31 to 2 - a 93% reduction.
Backward Compatibility
Testing
Fixes #7593
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
s01.oss.sonatype.org
/usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED -XX:MaxMetaspaceSize=384m -XX:+HeapDumpOnOutOfMemoryError -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.14-bin/38aieal9i53h9rfe7vjup95b9/gradle-8.14/lib/gradle-daemon-main-8.14.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.14-bin/38aieal9i53h9rfe7vjup95b9/gradle-8.14/lib/agents/gradle-instrumentation-agent-8.14.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.14
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.