perf: split attribution_enforced_versions subquery to use indexes

The original view used a single correlated `not exists` with an `or`,
which prevented the partial unique indexes on `attributions_exemptions`
from being used. Splitting into two `not exists` clauses lets each
anti-join use its own index instead of a full sequential scan.
This commit is contained in:
Michael H.
2026-06-24 00:19:16 +02:00
parent 7bf85239de
commit d1e8a9c574
@@ -0,0 +1,20 @@
-- The original view used a single correlated `not exists` with an `or`, which
-- prevents the partial unique indexes on `attributions_exemptions (version_id)`
-- and `(project_id)` from being used, forcing a full sequential scan of the
-- exemptions table for every candidate version.
--
-- `not exists (a or b)` is equivalent to `not exists (a) and not exists (b)`,
-- so splitting the subquery lets each anti-join use its own index.
create or replace view attribution_enforced_versions as
select v.id
from versions v
where not exists (
select 1
from attributions_exemptions ae
where ae.version_id = v.id
)
and not exists (
select 1
from attributions_exemptions ae
where ae.project_id = v.mod_id
);