Commit ada864e
committed
perf(core): add composite index on notifications to fix slow unread count queries
Fixes #3952. On forums with large notification tables, the unread/new
notification count queries (serialized on every page load via
CurrentUserSerializer) were performing a full table scan. MySQL abandons
the single-column user_id index when one user owns a significant fraction
of the table, which is common on active forums.
The fix adds a composite index (user_id, is_deleted, read_at, type) so
MySQL can use a range scan on (user_id, is_deleted=0, read_at IS NULL)
and satisfy the entire count query from the index without scanning the
table. In the realistic production case (a user with many read
notifications and a small number unread), this reduces the rows examined
from ~1M to the exact unread count.
Benchmarked against a 1M-row table with 200k notifications for one user
(5k users total, two notification subject types to exercise both
visibility scoping branches):
Realistic case (500 unread / 200k total for user):
Before: 86ms — full table scan, ~1M rows examined
After: 3ms — index range scan, 500 rows examined (96% faster)
Worst case (200k unread / 200k total for user):
Before: 106ms — full table scan, ~1M rows examined
After: 124ms — index range scan, ~200k rows examined
The worst case (user has never read any notification) shows a slight
regression because the index scan still visits 200k rows. This scenario
is not realistic in production, and on a cold disk-backed database the
index would still outperform the full scan.
No code changes — visibility scoping is fully retained and there are no
breaking changes for extensions.1 parent cfb85d1 commit ada864e
2 files changed
Lines changed: 532 additions & 0 deletions
File tree
- framework/core
- migrations
- tests/integration/notification
Lines changed: 39 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
0 commit comments