Skip to content

Optimize homepage post loading by eliminating N+1 queries for user data #7668

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

Merged
merged 1 commit into from
Aug 6, 2025

Conversation

JohnNiang
Copy link
Member

@JohnNiang JohnNiang commented Aug 6, 2025

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?

优化首页、归档页和作者页加载速度

@f2c-ci-robot f2c-ci-robot bot added kind/improvement Categorizes issue or PR as related to a improvement. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 6, 2025
@f2c-ci-robot f2c-ci-robot bot added this to the 2.21.x milestone Aug 6, 2025
@f2c-ci-robot f2c-ci-robot bot added the area/core Issues or PRs related to the Halo Core label Aug 6, 2025
@f2c-ci-robot f2c-ci-robot bot requested review from guqing and ruibaby August 6, 2025 10:40
Copy link

sonarqubecloud bot commented Aug 6, 2025

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes homepage and archive page loading performance by eliminating N+1 database queries when fetching post data and associated metadata (users, tags, categories, counters). Instead of making individual queries for each post's related data, the implementation now batches these queries by collecting all required IDs upfront and fetching them in bulk operations.

Key changes:

  • Introduced batch query methods for users, tags, categories, and counters
  • Refactored post list conversion to use bulk operations instead of sequential individual queries
  • Added ghost user support for missing user references

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
PostPublicQueryServiceImpl.java Core optimization - replaced sequential queries with bulk batch operations for post list conversion
UserServiceImpl.java Added bulk user query method and moved ghost user constant to interface
CounterServiceImpl.java Added batch counter query functionality
TagFinderImpl.java Replaced sequential queries with single bulk query using IN operator
CategoryFinderImpl.java Replaced sequential queries with single bulk query using IN operator
ContributorFinderImpl.java Updated to use new bulk user service method
ContributorVo.java Added static ghost user factory method
PostFinderImpl.java Updated to use new batch conversion method
Interface files Updated method signatures to accept Collection instead of List for better flexibility

Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Aug 6, 2025
Copy link

f2c-ci-robot bot commented Aug 6, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ruibaby

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 6, 2025
@f2c-ci-robot f2c-ci-robot bot merged commit 3a50fdc into main Aug 6, 2025
9 checks passed
@JohnNiang JohnNiang deleted the perf/index-page-queries branch August 6, 2025 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/improvement Categorizes issue or PR as related to a improvement. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request to reduce SQL query count for homepage posts.
2 participants