Skip to content
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

sqlite: Optimize the DB and the files sizes #4602

Merged
merged 4 commits into from
Feb 3, 2025

Conversation

zecakeh
Copy link
Collaborator

@zecakeh zecakeh commented Feb 1, 2025

  • First commit: run VACUUM on the SqliteStateStore. We should have done this when we moved the media cache to SqlitEventCacheStore, to defragment the file and reduce its size on the filesystem. Because of that there are databases from that time that still take much more space than they need.
  • Second commit: run PRAGMA optimize after running the eventual migrations. The SQLite docs recommend to do this regularly, especially after a schema change. Doing this consistently during construction seems to be the easiest solution.
  • Third commit: limit the size of the WAL file. While the DB connections of the databases are open, the size of the WAL file can keep increasing depending on the size needed for the transactions. A critical case is VACUUM which basically writes the content of the DB file to the WAL file before writing it back to the DB file, so we end up taking twice the size of the database. The docs say that the WAL file is written to the database when it reaches 4MB, so 10MB seems like a good limit that shouldn't truncate the file too often.

It should have been done in the migration of version 7, to reduce the
size of the database on the filesystem after the media cache was moved
to the SqliteEventCacheStore. Better late than never.

Signed-off-by: Kévin Commaille <[email protected]>
@zecakeh zecakeh requested a review from a team as a code owner February 1, 2025 15:11
@zecakeh zecakeh requested review from poljar and removed request for a team February 1, 2025 15:11
Copy link

codecov bot commented Feb 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.73%. Comparing base (57919f5) to head (c857daf).
Report is 8 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4602   +/-   ##
=======================================
  Coverage   85.73%   85.73%           
=======================================
  Files         291      291           
  Lines       33288    33303   +15     
=======================================
+ Hits        28538    28552   +14     
- Misses       4750     4751    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

As recommended by the SQLite docs.

Signed-off-by: Kévin Commaille <[email protected]>
@zecakeh zecakeh force-pushed the optimize-sqlite branch 2 times, most recently from 2e091d8 to 8222a7d Compare February 1, 2025 16:10
The WAL file can grow depending on the transactions that are run. A
critical case is VACUUM which basically writes the content of the DB
file to the WAL file before writing it back to the DB file.

SQLite doesn't try to reduce the size of the file after that unless we
set an explicit limit,
so we could end up taking twice the size of the database on the
filesystem.

Signed-off-by: Kévin Commaille <[email protected]>
Signed-off-by: Kévin Commaille <[email protected]>
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Thanks for the comprehensive links to the SQLite documentation.

@poljar poljar merged commit df3cb00 into matrix-org:main Feb 3, 2025
40 checks passed
@zecakeh zecakeh deleted the optimize-sqlite branch February 3, 2025 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants