Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-01-20 - Avoid repository pollution with benchmark scripts
**Learning:** Checking in temporary test scripts and binary dummy databases (like `benchmark.py` and `test.db`) during development is considered a blocking issue for merges as it pollutes the repository.
**Action:** Always place temporary testing artifacts in `/tmp/` and clean them up before requesting code review or submitting changes.
18 changes: 13 additions & 5 deletions src-tauri/src/library/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,16 +946,21 @@ impl Repository {
let tx = conn.transaction()?;

let mut stmt = tx.prepare(
// Performance optimization: Avoid correlated scalar subqueries in the SELECT clause (EXISTS, COUNT)
// during unpaginated full-table scans. Using Early Aggregation via derived tables prevents
// an N+1 execution bottleneck.
"SELECT
b.id,
b.title,
b.authors_json,
b.isbn10,
b.isbn13,
b.updated_at,
EXISTS(SELECT 1 FROM manual_overrides mo WHERE mo.book_id = b.id) AS has_manual_overrides,
(SELECT COUNT(*) FROM book_files bf WHERE bf.book_id = b.id) AS file_count
FROM books b",
mo.has_manual IS NOT NULL AS has_manual_overrides,
COALESCE(bf.file_count, 0) AS file_count
FROM books b
LEFT JOIN (SELECT book_id, 1 AS has_manual FROM manual_overrides GROUP BY book_id) mo ON mo.book_id = b.id
LEFT JOIN (SELECT book_id, COUNT(*) AS file_count FROM book_files GROUP BY book_id) bf ON bf.book_id = b.id",
)?;

let mut candidates = Vec::new();
Expand Down Expand Up @@ -1339,9 +1344,12 @@ impl Repository {
(SELECT COUNT(*) FROM book_files bf WHERE bf.book_id = lb.id) AS file_count
FROM limited_books lb"
} else {
// Performance optimization: Avoid correlated scalar subqueries (COUNT) in the SELECT
// clause during large unpaginated table scans. Early Aggregation prevents an N+1 bottleneck.
"SELECT b.id, b.title, b.authors_json,
(SELECT COUNT(*) FROM book_files bf WHERE bf.book_id = b.id) AS file_count
FROM books b"
COALESCE(bf.file_count, 0) AS file_count
FROM books b
LEFT JOIN (SELECT book_id, COUNT(*) AS file_count FROM book_files GROUP BY book_id) bf ON bf.book_id = b.id"
};
let mut stmt = conn.prepare(query)?;

Expand Down