perf(backend): optimize drive/files query for old root content#16514
perf(backend): optimize drive/files query for old root content#16514caipira113 wants to merge 2 commits intomisskey-dev:developfrom
Conversation
|
このPRによるapi.jsonの差分 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #16514 +/- ##
===========================================
+ Coverage 42.56% 46.06% +3.49%
===========================================
Files 1685 1771 +86
Lines 170650 182361 +11711
Branches 4223 5402 +1179
===========================================
+ Hits 72643 84009 +11366
- Misses 97546 98323 +777
+ Partials 461 29 -432 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Is this because of the ASC/DESC? It seems the current index is If the reason is index order we should probably create index for Or maybe just simply CHANGE IDX_860fa6f6c7df5bb887249fba22 |
|
Thanks for the comment @eternal-flame-AD |
|
Thanks! I wonder why postgres refuse to use the original index then .. but yes .. I think (userId, id) should do because there is a practical limit on how many files a single user can create without it being abusing the service so the worst case performance of index scan over list of all files owned by a specific user in ID order should be good enough. Doesn't seem like there is currently a role limit on # of drive files, but I would say it is more useful to add a limit than to try to optimize the query for that case-because that requires the file storage logic to not rely on the file system (which is basically another index) as well. |
0d5a916 to
fcfbf4a
Compare
fcfbf4a to
c0b380b
Compare
What
Resolve #14217
This PR speeds up the
drive/filesendpoint for users who have old content in the root folder by adding a partial index ondrive_file (userId, folderId IS NULL, id DESC). There are no API or behavior changes; it only adds a migration.Why
Some users were hitting statement timeouts because PostgreSQL chose the primary key for
ORDER BY id DESC, leading to very wide scans when their root files were old. The new index matches the query shape so the planner can return the latest matching rows immediately.Additional info (optional)
On an affected account, the query improved from about 50 seconds to about 30-40 ms.
[ TEST Query ]
[ Before ]
2025-09-03.11.52.56-1.mov
[ After ]
2025-09-03.11.54.19-1.mov
Checklist