Get all tie-in records that DON'T exist (opposite/inverse of inner join)

I feel like the gist describes this pretty well. Although even now, the inner join query takes 12 seconds to run (perhaps we need better indexing?). I’m not super excited about that part and would love to make things faster…

Thoughts?

Okay, so to get all combinations (currently have many of these), I can run this query:

select
    m.manufacturerId as manufacturerId, d.defectId as defectId
from productionlines as d
inner join productionlines as m on m.type="manufacturer"
where d.type="defect"
order by m.manufacturerId

Now, if I could filter out of this list existing manufacturer defects… then return what remains…

STEP 1: Find all combinations.

STEP 2: Exclude all existing manufacturing defects.

(select
    m.manufacturerId as manufacturerId, d.defectId as defectId
from productionlines as d
inner join productionlines as m on m.type="manufacturer"
where d.type="defect"
order by m.manufacturerId)
EXCEPT
(select
    md.f_manufacturerId as manufacturerId, md.f_defectId as defectId
from productionlines as md
where md.type="manufacturerdefect")
order by manufacturerId