SQLite bookmark creation: URL lookup plan improves after ANALYZE
On a 10,000-bookmark SQLite fixture, bookmark creation in the unchanged Python linkding v1.47.0 image became faster after running ANALYZE once on a copy of the database. The experiment changed no linkding code or application schema; it populated SQLite planner statistics.
Proposed change
Add an opt-in, SQLite-only Django management command that runs ANALYZE on linkding's main database, and document when an operator might run it after a large import or substantial database growth. Keep the existing bookmark lookup query and indexes unchanged. I suggest an explicit maintenance command rather than running ANALYZE on every startup; its execution time and locking impact still need measurement before a PR. Does that approach fit the project?
The fixture had no sqlite_stat1 rows. For a URL that was not already bookmarked, EXPLAIN QUERY PLAN showed SQLite selecting the owner index and scanning that user's bookmarks, although an index on the normalized URL exists. After ANALYZE, the same query selected the normalized-URL index. In an isolated probe, 2,000 URL misses took 2.493 seconds before and 0.031 seconds after ANALYZE.
I then ran the unmodified Python image against separate plain and analyzed copies of the same fixture. Each point had 200 warmup creates and 1,000 measured creates; the table shows medians of three repeats:
| Concurrent clients | Before ANALYZE |
After ANALYZE |
|---|---|---|
| 8 | 347.6 requests/s | 412.0 requests/s (+18.5%) |
| 32 | 349.6 requests/s | 397.1 requests/s (+13.6%) |
All create requests returned HTTP 201, and SQLite integrity checks passed. The analysis and reproduction details and raw results are available. This measures one local fixture and does not establish a benefit for every installation; I have not measured the cost or best timing of running ANALYZE in a live server.
If this approach is useful, I can measure the command's cost and prepare a focused PR.
Code pointers
bookmarks/models.py:Bookmark.query_existingand the indexedurl_normalizedfield.
Source: sissbruecker/linkding