From 0efba945bac9b84a489c6ef552e834593f209fe1 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 15 May 2025 15:44:55 +0200 Subject: [PATCH] fix: improve perf of job deletion --- backend/src/monitor.rs | 12 ++++++------ backend/windmill-api/src/db.rs | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/src/monitor.rs b/backend/src/monitor.rs index cf04ff8c32..f354249ff1 100644 --- a/backend/src/monitor.rs +++ b/backend/src/monitor.rs @@ -62,7 +62,11 @@ use windmill_common::{ users::truncate_token, utils::{empty_as_none, now_from_db, rd_string, report_critical_error, Mode}, worker::{ - load_env_vars, load_init_bash_from_env, load_whitelist_env_vars_from_env, load_worker_config, reload_custom_tags_setting, store_pull_query, store_suspended_pull_query, update_min_version, Connection, WorkerConfig, DEFAULT_TAGS_PER_WORKSPACE, DEFAULT_TAGS_WORKSPACES, INDEXER_CONFIG, SCRIPT_TOKEN_EXPIRY, SMTP_CONFIG, TMP_DIR, WORKER_CONFIG, WORKER_GROUP + load_env_vars, load_init_bash_from_env, load_whitelist_env_vars_from_env, + load_worker_config, reload_custom_tags_setting, store_pull_query, + store_suspended_pull_query, update_min_version, Connection, WorkerConfig, + DEFAULT_TAGS_PER_WORKSPACE, DEFAULT_TAGS_WORKSPACES, INDEXER_CONFIG, SCRIPT_TOKEN_EXPIRY, + SMTP_CONFIG, TMP_DIR, WORKER_CONFIG, WORKER_GROUP, }, KillpillSender, BASE_URL, CRITICAL_ALERTS_ON_DB_OVERSIZE, CRITICAL_ALERT_MUTE_UI_ENABLED, CRITICAL_ERROR_CHANNELS, DB, DEFAULT_HUB_BASE_URL, HUB_BASE_URL, JOB_RETENTION_SECS, @@ -829,11 +833,7 @@ pub async fn delete_expired_items(db: &DB) -> () { Ok(mut tx) => { let deleted_jobs = sqlx::query_scalar!( "DELETE FROM v2_job_completed c - USING v2_job j - WHERE - created_at <= now() - ($1::bigint::text || ' s')::interval - AND completed_at + ($1::bigint::text || ' s')::interval <= now() - AND c.id = j.id + WHERE completed_at + ($1::bigint::text || ' s')::interval <= now() RETURNING c.id", job_retention_secs ) diff --git a/backend/windmill-api/src/db.rs b/backend/windmill-api/src/db.rs index 557981d214..daa7a9b0e7 100644 --- a/backend/windmill-api/src/db.rs +++ b/backend/windmill-api/src/db.rs @@ -782,6 +782,14 @@ async fn fix_job_completed_index(db: &DB) -> Result<(), Error> { .execute(db) .await?; }); + + run_windmill_migration!("job_completed_completed_at", db, |tx| { + sqlx::query!( + "CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_completed_completed_at ON v2_job_completed (completed_at DESC)" + ) + .execute(db) + .await?; + }); Ok(()) }