Improve ML model loading (#22073)
* Optimize ML model query and binding logic - Refactor `db_models_load` to retrieve the most recent models first with a limit and sort them ascending by `after`. * Fix indentation in `ml_dimension_stream_kmeans` call for better readability * Refactor `db_models_load` query to prioritize sorting by `before` timestamp for improved model ordering consistency. * Refactor ML model query to explicitly define selected columns for improved clarity and performance. * Tune the query to simplify ordering logic and improve readability.
Stelios Fragkakis committed
Mar 29, 2026 at 22:25 UTC
56852a144e809c9d1489f367fb39124104e66daf
1 file changed
+10
-1
src/ml/ml.cc
+10
-1
@@ -168,10 +168,15 @@ const char *db_models_add_model =
168
" @c10, @c11, @c12, @c13, @c14, @c15);";
169
170
const char *db_models_load =
171
+ "SELECT after, before, min_dist, max_dist, "
172
+ "c00, c01, c02, c03, c04, c05, "
173
+ "c10, c11, c12, c13, c14, c15 FROM ("
174
"SELECT after, before, min_dist, max_dist, "
175
"c00, c01, c02, c03, c04, c05, "
176
"c10, c11, c12, c13, c14, c15 FROM models "
174
- "WHERE dim_id = @dim_id AND after >= @after ORDER BY before ASC;";
177
+ "WHERE dim_id = @dim_id AND after >= @after "
178
+ "ORDER BY after DESC LIMIT @n"
179
+ ") ORDER BY after ASC;";
180
181
const char *db_models_delete =
182
"DELETE FROM models "
@@ -403,6 +408,10 @@ int ml_dimension_load_models(RRDDIM *rd, sqlite3_stmt **active_stmt) {
408
if (unlikely(rc != SQLITE_OK))
409
goto bind_fail;
410
411
+ rc = sqlite3_bind_int64(res, ++param, Cfg.num_models_to_use);
412
+ if (unlikely(rc != SQLITE_OK))
413
+ goto bind_fail;
414
+
415
spinlock_lock(&dim->slock);
416
417
dim->km_contexts.reserve(Cfg.num_models_to_use);