-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Perf: add extra index to notification table #32395
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
+127
−9
Conversation
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
Migrations are necessary. You need add one line in |
lunny
reviewed
Nov 1, 2024
lunny
reviewed
Nov 1, 2024
a529100
to
1283d41
Compare
lunny
reviewed
Nov 1, 2024
1283d41
to
09c4ea7
Compare
lunny
reviewed
Nov 1, 2024
lunny
reviewed
Nov 1, 2024
lunny
reviewed
Nov 1, 2024
lunny
approved these changes
Nov 2, 2024
Please resolve the conflict and I cannot push to your branch. |
58b5203
to
949d4b2
Compare
949d4b2
to
4631f75
Compare
Thank you for the notice! Done. |
bohde
approved these changes
Nov 13, 2024
zjjhot
added a commit
to zjjhot/gitea
that referenced
this pull request
Nov 14, 2024
* giteaofficial/main: Refactor render system (go-gitea#32492) Fix nil panic if repo doesn't exist (go-gitea#32501) Bump CI,Flake and Snap to Node 22 (go-gitea#32487) Perf: add extra index to notification table (go-gitea#32395) Fix LFS route mock, realm, middleware names (go-gitea#32488)
DennisRasey
pushed a commit
to DennisRasey/forgejo
that referenced
this pull request
Dec 6, 2024
- For the notifications page the unread and pinned notifications are gathered for doer those that and are ordered by the updated unix. MariaDB makes a bad decision (sometimes, for most users it does not make this decision) with this query, it uses the index for the `updated_unix` column to speed up this query, however this is not the correct index to be taking, if the doer does not have more than 20 (the page size) unread and pinned notifications combined MariaDB will traverse the whole notifications table before it realizes that there are no more notifications to be gathered. It instead should use the index for the `user_id` column (this is what MariaDB already does for most users), so the list that has to be traversed is limited to the doer's notifications which is significantly less than the whole notifications table. - This is a different approach than what Gitea has taken to solve this problem, which is to add a index to the (status, userid, updated_unix) tuple (Ref: go-gitea/gitea#32395). Adding more and more indexes is not a good way if we can use existing indexes to get a query to a acceptable performance. - The code cannot use `db.Find` as it's hard to add a index hint option specifically for this query and not for the other instances that uses `activities_model.FindNotificationOptions`. - Only add a index hint for MySQL as I have not been able to test if SQLite or PostgreSQL are smart enough to use the better index (as you need a large enough dataset to test this meaningfully). - Integration test added to ensure the SQL is run by all databases. --- Performance numbers (from Codeberg's database - MariaDB 10.11.6-MariaDB-0+deb12u1): Currently: ```sql SELECT * FROM `notification` WHERE notification.user_id=26734 AND (notification.status=3 OR notification.status=1) ORDER BY notification.updated_unix DESC LIMIT 20; (5.731 sec) +------+-------------+--------------+-------+--------------------------------------------------+-------------------------------+---------+-------+---------+------------+----------+------------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | r_rows | filtered | r_filtered | Extra | +------+-------------+--------------+-------+--------------------------------------------------+-------------------------------+---------+-------+---------+------------+----------+------------+-------------+ | 1 | SIMPLE | notification | index | IDX_notification_status,IDX_notification_user_id | IDX_notification_updated_unix | 8 | const | 1376836 | 1474066.00 | 50.03 | 0.00 | Using where | +------+-------------+--------------+-------+--------------------------------------------------+-------------------------------+---------+-------+---------+------------+----------+------------+-------------+ ``` Using the better index: ```sql SELECT * FROM `notification` USE INDEX (IDX_notification_user_id) WHERE notification.user_id=26734 AND (notification.status=3 OR notification.status=1) ORDER BY notification.updated_unix DESC LIMIT 20; (0.834 sec) +------+-------------+--------------+--------+----------------------------------------------------------+--------------------------+---------+----------------------------------+-------+----------+----------+------------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | r_rows | filtered | r_filtered | Extra | +------+-------------+--------------+--------+----------------------------------------------------------+--------------------------+---------+----------------------------------+-------+----------+----------+------------+----------------------------------------------+ | 1 | PRIMARY | notification | ref | PRIMARY,IDX_notification_status,IDX_notification_user_id | IDX_notification_user_id | 8 | const | 22042 | 10756.00 | 50.03 | 0.02 | Using where; Using temporary; Using filesort | | 1 | PRIMARY | notification | eq_ref | PRIMARY | PRIMARY | 8 | gitea_production.notification.id | 1 | 1.00 | 100.00 | 100.00 | | +------+-------------+--------------+--------+----------------------------------------------------------+--------------------------+---------+----------------------------------+-------+----------+----------+------------+----------------------------------------------+ ```
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
lgtm/done
This PR has enough approvals to get merged. There are no important open reservations anymore.
modifies/go
Pull requests that update Go code
modifies/migrations
performance/speed
performance issues with slow downs
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.
Index SQL:
CREATE INDEX u_s_uu ON notification(user_id, status, updated_unix);
The naming follows
action.go
in the same dir.I am unsure which version I should add SQL to the migration folder, so I have not modified it.
Fix #32390