Skip to content

Commit ee25dfa

Browse files
committed
perf: optimize on conn close
- SQLite recommends running PRAGMA optimize on close to boost query perf so we will do what SQLite recommends :)
1 parent 2836f9e commit ee25dfa

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

crates/sqlx-sqlite-conn-mgr/src/database.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,16 @@ impl SqliteDatabase {
144144
drop(conn); // Close immediately after creating the file
145145
}
146146

147+
// Enable PRAGMA optimize on close as recommended by SQLite for long-lived databases.
148+
// SQLite recommends analysis_limit values between 100-1000 for older versions;
149+
// SQLite 3.46.0+ handles limits automatically.
150+
// https://www.sqlite.org/lang_analyze.html#recommended_usage_pattern
151+
//
147152
// Create read pool with read-only connections
148-
let read_options = SqliteConnectOptions::new().filename(&path).read_only(true);
153+
let read_options = SqliteConnectOptions::new()
154+
.filename(&path)
155+
.read_only(true)
156+
.optimize_on_close(true, 400);
149157

150158
let read_pool = SqlitePoolOptions::new()
151159
.max_connections(config.max_read_connections)
@@ -157,7 +165,10 @@ impl SqliteDatabase {
157165
.await?;
158166

159167
// Create write pool with a single read-write connection
160-
let write_options = SqliteConnectOptions::new().filename(&path).read_only(false);
168+
let write_options = SqliteConnectOptions::new()
169+
.filename(&path)
170+
.read_only(false)
171+
.optimize_on_close(true, 400);
161172

162173
let write_conn = SqlitePoolOptions::new()
163174
.max_connections(1)

0 commit comments

Comments
 (0)