Conversation
ac74f67 to
85fdc95
Compare
0a6aff5 to
bff5133
Compare
adbenitez
approved these changes
Mar 26, 2026
Collaborator
adbenitez
left a comment
There was a problem hiding this comment.
this fixed the problem!
With `ORDER BY` statement SQLite searches the `imap` table by `transport_id` and for each found row scans the whole `imap_markseen` table. Number of `imap` entries for each `transport_id` is usually large as we need to know which UIDs to delete on IMAP server when deleting a message. ``` sqlite> EXPLAIN QUERY PLAN SELECT imap.id, uid, folder FROM imap, imap_markseen WHERE imap.id = imap_markseen.id AND imap.transport_id=? AND target = folder ORDER BY folder, uid; QUERY PLAN |--SEARCH imap USING INDEX sqlite_autoindex_imap_1 (transport_id=?) `--SCAN imap_markseen ``` Without `ORDER BY` statement SQLite scans `imap_markseen` table which is expected to be small, and then searches `imap` table by `rowid` for each found result. ``` sqlite> EXPLAIN QUERY PLAN SELECT imap.id, uid, folder FROM imap, imap_markseen WHERE imap.id = imap_markseen.id AND imap.transport_id=? AND target = folder; QUERY PLAN |--SCAN imap_markseen `--SEARCH imap USING INTEGER PRIMARY KEY (rowid=?) ``` Query planning was tested with SQLite 3.52.0. It is possible to explictly make query planner move sorting to the last step with `ORDER +folder, +uid`, but this is not recommended in SQLite documentation (see <https://www.sqlite.org/optoverview.html#uplus>). It is also possible to add indexes, but indexes use space, adding them requires an SQL migration, and each index needs to be updated so it will slow down writes.
…ekeeping Previously transports deleted via sync messages left unused `imap` entries.
bff5133 to
8ddd12a
Compare
iequidoo
approved these changes
Mar 26, 2026
| // We are sorting outside of SQL to avoid SQLite constructing a query plan | ||
| // that scans the whole `imap` table. Scanning `imap_markseen` is fine | ||
| // as it should not have many items. | ||
| // If you change the SQL query, test it with `EXPLAIN QUERY PLAN`. |
Collaborator
There was a problem hiding this comment.
This should goto STYLE.md i think
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #8022