refactor(author): count poems with left join and group by#66
Merged
Conversation
hawk-roy
force-pushed
the
for_author_pref
branch
from
July 14, 2026 16:40
22e2561 to
5f1b5be
Compare
palemoky
self-requested a review
July 15, 2026 00:06
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
Owner
|
感谢 PR!先说结论:改动前后的查询完全一致,但在性能上略微下降,为了提高维护性与风格一致性,可以合并。 以下是我的测试结果: 测试环境:
测试结果:
为什么更慢?
另外,由于有 |
palemoky
approved these changes
Jul 15, 2026
palemoky
left a comment
Owner
There was a problem hiding this comment.
虽然性能有所下降,但未来数据几乎不会增长,为了保持代码风格一致性与可维护性,准许合并。
Contributor
Author
|
感谢您的合并和快速反馈!您补充的子查询与 JOIN 查询的实际执行结果对我很有帮助,也让我更清楚这个改动在当前数据规模下的性能影响。有点酷! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR optimizes
GetAuthorsWithStatsby changing howpoem_countis calculated for authors.Previously, the query used a correlated subquery to count poems for each author row. That means the database may need to evaluate the count repeatedly as the author result set grows.
This change replaces it with a
LEFT JOINplusGROUP BY, so poem counts are calculated through one aggregate query while preserving the existing API behavior.Why This Change
The previous approach was simple, but it could become inefficient on larger datasets because the poem count was calculated separately for each author.
Using
LEFT JOINandGROUP BYlets the database compute poem counts in a more set-oriented way. This is generally easier for the query optimizer to handle, especially whenpoems.author_idis indexed.What Changed
COUNT(poems.id)to calculate each author's poem count.LEFT JOINso authors without poems are still included.GROUP BY authors.idto aggregate poem counts by author.Behavior Preserved
poem_count DESC.limitandoffsetis unchanged.Testing
Tested locally and confirmed the author stats API still returns the expected results.