From 915bc4abdab222f8ecfab7402746fcf9f3018b6f Mon Sep 17 00:00:00 2001 From: Jack Lynch Date: Wed, 20 May 2026 15:01:07 +0100 Subject: [PATCH] =?UTF-8?q?FDN-4939:=20Fix=20NOT=20IN=20=E2=86=92=20NOT=20?= =?UTF-8?q?EXISTS=20in=20ScheduleMigrateVersionsProcessor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit NOT IN with a subquery was causing ~99% CPU on apibuilderdb20211217. The subquery cast type_id::uuid per row, defeating the index, and the NOT IN semantics materialise the full subquery before filtering. NOT EXISTS with a correlated predicate (type_id = v.guid::text) lets Postgres use the existing unique index on tasks(type_id, type) for a targeted seek per outer row instead of a full subquery scan. Co-Authored-By: Claude Sonnet 4.6 --- .../ScheduleMigrateVersionsProcessor.scala | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/api/app/processor/ScheduleMigrateVersionsProcessor.scala b/api/app/processor/ScheduleMigrateVersionsProcessor.scala index d25d858aa..c047fb271 100644 --- a/api/app/processor/ScheduleMigrateVersionsProcessor.scala +++ b/api/app/processor/ScheduleMigrateVersionsProcessor.scala @@ -28,14 +28,20 @@ class ScheduleMigrateVersionsProcessor @Inject()( s""" |select v.guid | from versions v - | join applications apps on apps.guid = v.application_guid and apps.deleted_at is null + | join applications apps on apps.guid = v.application_guid + | and apps.deleted_at is null | left join cache.services on services.deleted_at is null - | and services.version_guid = v.guid - | and services.version = {service_version} + | and services.version_guid = v.guid + | and services.version = {service_version} | where v.deleted_at is null | and services.guid is null - | and v.guid not in (select type_id::uuid from tasks where type = {task_type}) - |limit $Limit + | and not exists ( + | select 1 + | from tasks + | where type = {task_type} + | and type_id = v.guid::text + | ) + | limit $Limit |""".stripMargin ).bind("service_version", MigrateVersion.ServiceVersionNumber) .bind("task_type", TaskType.MigrateVersion.toString)