mirror of
https://github.com/go-gitea/gitea.git
synced 2026-03-10 22:06:34 -05:00
Feature Request for better performance on paging and lees memory usage on database #13352
Closed
opened 2025-11-02 10:39:26 -06:00 by GiteaMirror
·
26 comments
No Branch/Tag Specified
main
release/v1.25
release/v1.24
release/v1.23
release/v1.22
release/v1.21
release/v1.20
release/v1.19
release/v1.18
release/v1.17
release/v1.16
release/v1.15
release/v1.14
release/v1.13
release/v1.12
release/v1.11
release/v1.10
release/v1.9
release/v1.8
v1.25.3
v1.25.2
v1.25.1
v1.25.0
v1.24.7
v1.25.0-rc0
v1.26.0-dev
v1.24.6
v1.24.5
v1.24.4
v1.24.3
v1.24.2
v1.24.1
v1.24.0
v1.23.8
v1.24.0-rc0
v1.25.0-dev
v1.23.7
v1.23.6
v1.23.5
v1.23.4
v1.23.3
v1.23.2
v1.23.1
v1.23.0
v1.23.0-rc0
v1.24.0-dev
v1.22.6
v1.22.5
v1.22.4
v1.22.3
v1.22.2
v1.22.1
v1.22.0
v1.23.0-dev
v1.22.0-rc1
v1.21.11
v1.22.0-rc0
v1.21.10
v1.21.9
v1.21.8
v1.21.7
v1.21.6
v1.21.5
v1.21.4
v1.21.3
v1.21.2
v1.20.6
v1.21.1
v1.21.0
v1.21.0-rc2
v1.21.0-rc1
v1.20.5
v1.22.0-dev
v1.21.0-rc0
v1.20.4
v1.20.3
v1.20.2
v1.20.1
v1.20.0
v1.19.4
v1.21.0-dev
v1.20.0-rc2
v1.20.0-rc1
v1.20.0-rc0
v1.19.3
v1.19.2
v1.19.1
v1.19.0
v1.19.0-rc1
v1.20.0-dev
v1.19.0-rc0
v1.18.5
v1.18.4
v1.18.3
v1.18.2
v1.18.1
v1.18.0
v1.17.4
v1.18.0-rc1
v1.19.0-dev
v1.18.0-rc0
v1.17.3
v1.17.2
v1.17.1
v1.17.0
v1.17.0-rc2
v1.16.9
v1.17.0-rc1
v1.18.0-dev
v1.16.8
v1.16.7
v1.16.6
v1.16.5
v1.16.4
v1.16.3
v1.16.2
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1.4.0-rc1
v1.3.2
v1.3.1
v1.3.0
v1.3.0-rc2
v1.3.0-rc1
v1.2.3
v1.2.2
v1.2.1
v1.2.0
v1.2.0-rc3
v1.2.0-rc2
v1.1.4
v1.2.0-rc1
v1.1.3
v1.1.2
v1.1.1
v1.1.0
v1.0.2
v1.0.1
v1.0.0
v0.9.99
Labels
Clear labels
$20
$250
$50
$500
backport/done
💎 Bounty
docs-update-needed
good first issue
hacktoberfest
issue/bounty
issue/confirmed
issue/critical
issue/duplicate
issue/needs-feedback
issue/not-a-bug
issue/regression
issue/stale
issue/workaround
lgtm/need 2
modifies/api
modifies/translation
outdated/backport/v1.18
outdated/theme/markdown
outdated/theme/timetracker
performance/bigrepo
performance/cpu
performance/memory
performance/speed
pr/breaking
proposal/accepted
proposal/rejected
pr/wip
pull-request
reviewed/wontfix
💰 Rewarded
skip-changelog
status/blocked
topic/accessibility
topic/api
topic/authentication
topic/build
topic/code-linting
topic/commit-signing
topic/content-rendering
topic/deployment
topic/distribution
topic/federation
topic/gitea-actions
topic/issues
topic/lfs
topic/mobile
topic/moderation
topic/packages
topic/pr
topic/projects
topic/repo
topic/repo-migration
topic/security
topic/theme
topic/ui
topic/ui-interaction
topic/ux
topic/webhooks
topic/wiki
type/bug
type/deprecation
type/docs
type/enhancement
type/feature
type/miscellaneous
type/proposal
type/question
type/refactoring
type/summary
type/testing
type/upstream
Mirrored from GitHub Pull Request
Milestone
No items
No Milestone
Projects
Clear projects
No project
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/gitea#13352
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @somera on GitHub (Aug 1, 2024).
Feature Description
I'm again. The gitea user with 3655 organisations and 22573 repos.
I wondered that my paging is slow. Not on the first pages.
First page need 1-3 seconds. 1 second if the data is precached.
On the last pages ...
But this is after my 1st optimisation.
You made this queries
on pagging.
Explain for the count query is
And than I changed the query to
SELECT count(action.id) FROM "action" INNER JOIN "repository" ON "repository".id = "action".repo_id WHERE user_id=$1 AND is_deleted=$2
and ...
it's faster.
Explain for the SELECT query is
very bad. I increased work_mem to 6GB and it's better now
After this fix it need ~11 seconds.
But it can be better. Cause the problem is this
SELECT action.* FROM action INNER JOIN repository ON repository.id = action.repo_id WHERE user_id='1' AND is_deleted='f' ORDER BY action.created_unix DESC LIMIT 20 OFFSET 2043740
query. When you change it to
SELECT action.id FROM action INNER JOIN repository ON repository.id = action.repo_id WHERE user_id='1' AND is_deleted='f' ORDER BY action.created_unix DESC LIMIT 20 OFFSET 2043740
-> you will get 20 action.id's than explain is
and need ~5 seconds and a very less memory on the database.
Than gitea should get the action.* infos only for the 20 id's in the new query.
This optimisation will be good for all gitea users.
Screenshots
No response
@somera commented on GitHub (Aug 1, 2024):
I know why it's getting slower.
https://readyset.io/blog/optimizing-sql-pagination-in-postgres
@somera commented on GitHub (Aug 1, 2024):
On slow paging I see this "cache context ..." log entries
@somera commented on GitHub (Aug 12, 2024):
Step 1:
OLD:
SELECT count(*) FROM "action" INNER JOIN "repository" ON "repository".id = "action".repo_id WHERE user_id=$1 AND is_deleted=$2NEW:
SELECT count(action.id) FROM "action" INNER JOIN "repository" ON "repository".id = "action".repo_id WHERE user_id=$1 AND is_deleted=$2Step 2:
OLD (expample with LIMIT and OFFSET -> SELECT action.* uses too much memory on database):
SELECT action.* FROM action INNER JOIN repository ON repository.id = action.repo_id WHERE user_id='1' AND is_deleted='f' ORDER BY action.created_unix DESC LIMIT 20 OFFSET 2043740NEW:
SELECT action.id FROM action INNER JOIN repository ON repository.id = action.repo_id WHERE user_id='1' AND is_deleted='f' ORDER BY action.created_unix DESC LIMIT 20 OFFSET 2043740SELECT action.* FROM action WHERE action.id IN (....)@lunny commented on GitHub (Aug 12, 2024):
Please can you help to confirm #31821 will resolve this issue?
@somera commented on GitHub (Aug 12, 2024):
@lunny yes, I can help. I can check it on my 2nd instance with the same account of data like my main instance.
I need an executable gitea-1.21.*-linux-amd64? Nigtly?
@lunny commented on GitHub (Aug 12, 2024):
No. It's just a PR. I don't think you can easily replace it to do a test and revert back easily.
@somera commented on GitHub (Aug 12, 2024):
Means I should compile it?
Tell me what I have to do? Cause I didn't compile gitea for my use with PostgreSQL.
@lunny commented on GitHub (Aug 14, 2024):
No. I can help to compile one but once your upgrade the version I provided, it's difficult for you to downgrade if there is no Gitea develop env for you.
@somera commented on GitHub (Aug 14, 2024):
I have 2nd gitea instance for testing. I can backup the database and after test restore the backup.
@lunny commented on GitHub (Aug 14, 2024):
Please test https://gitea.com/lunny/packages/src/branch/main/gitea-pr-31821-amd64-linux.tar.gz
@somera commented on GitHub (Aug 14, 2024):
@lunny thx. I tested this PR.
My 24/7 NUC with main gitea instance has an Intel Pentium Silver J5005 (4) @ 2.800GHz. The results in this PR was made on this NUC.
My test now I run on a faster Intel NUC with an Intel i5-8259U (8) @ 3.800GHz. Has more cores/threads and it's faster.
How I made the test.
My expectation is:
I see same projects on the same pages. But the order is reverted. Project which are on the 1. place on the page are in the PR version on the last place. But I see same projects!!!
Times:
@somera commented on GitHub (Aug 14, 2024):
Sorry, but I didn't open more page at the beginning. But the new version need less memory or tmp files in PostgreSQL as I show it in my description,
@somera commented on GitHub (Aug 14, 2024):
Last page in 1.22.1
Last page in 1.22.0+PR
As you see the order is reverted.
@somera commented on GitHub (Aug 14, 2024):
And on my slower NUC I have 11233 more pages.
I wait for you feedback before I restore my DB backup.
@lunny commented on GitHub (Aug 14, 2024):
Yes, it's a bug. Please try version 2 https://gitea.com/lunny/packages/src/branch/main/gitea-pr-31821-amd64-linux-2.tar.gz
@somera commented on GitHub (Aug 14, 2024):
I started this Version
1.22.0+rc1-415-gcd09625a76
and the sorting looks good. But I see the times are the same like in 1.22.1 and in PG log I see the action.* query
2024-08-15 00:09:56.653 CEST [6735] gitea@giteadb LOG: Dauer: 2259.538 ms Ausführen <unnamed>: SELECT "action".* FROM "action" INNER JOIN "repository" ON "repository".id = "action".repo_id WHERE user_id=$1 AND is_deleted=$2 ORDER BY "action"."created_unix" DESC LIMIT 20 OFFSET 1819340The second statement should be SELECT action.id FROM ...
Like here https://github.com/go-gitea/gitea/issues/31752#issuecomment-2283642707
In the first version it was ok
@lunny commented on GitHub (Aug 15, 2024):
In this version, it will use the old code when the page less than 10, otherwise it will use the new code.
@somera commented on GitHub (Aug 15, 2024):
@lunny sounds good, but when you see the OFFSET
2024-08-15 00:09:56.653 CEST [6735] gitea@giteadb LOG: Dauer: 2259.538 ms Ausführen <unnamed>: SELECT "action".* FROM "action" INNER JOIN "repository" ON "repository".id = "action".repo_id WHERE user_id=$1 AND is_deleted=$2 ORDER BY "action"."created_unix" DESC LIMIT 20 OFFSET 1819340SELECT action.* should be uses, cause I'm over 10 pages. And the new method was not used.
@lunny commented on GitHub (Aug 16, 2024):
:(. I made a mistake. version 3 https://gitea.com/lunny/packages/src/branch/main/gitea-pr-31821-amd64-linux-3.tar.gz
@somera commented on GitHub (Aug 16, 2024):
Looks good.
Page 1-9 old method. Pages 10+ new method. It was good idea to do it.
And the sorting looks good too.
@somera commented on GitHub (Aug 16, 2024):
@lunny can I restore my datanse now? Or should I wait?
@lunny commented on GitHub (Aug 16, 2024):
Yes, you can restore now. Thank you for your testing.
@somera commented on GitHub (Aug 16, 2024):
I have to thank you for the implementation of the PR.
@somera commented on GitHub (Oct 9, 2024):
@lunny I installed 1.22.3 release. I saw the changes.
I see this
SQL on paging.
I was sure, the change will be:
which in my case need 50% of the time in the higher pages and uses only 10% of the memory.
vs
@lunny commented on GitHub (Oct 11, 2024):
Please follow #32224
@somera commented on GitHub (Nov 25, 2024):
Just update for the start page ...
thx!