I am migrating person from a view to my database with the following query, which is too slow:
SELECT DISTINCT PI.VALUE ID
FROM PERSON_VIEW PERV
inner join PERSON_IDENT PI on PI.VALUE = PERV.IDENTITY
inner join PERSON PER on PER.ID = PI.EXTERNAL_ID
WHERE NOT EXISTS (SELECT RECORD_ID FROM PERSON_MIGR_DATA PMD
WHERE RECORD_ID = PERV.RECORD_ID)
AND NOT EXISTS (SELECT RECORD_ID FROM PERSON_MIGR_ERRORS
WHERE RECORD_ID = PERV.RECORD_ID)
There are indexes on joined fields, but I must change this query, as it lasts 15minutes.. Tables are quite big.
So. I have PERSON_VIEW
- from which I read person to migrate, PERSON_MIGR_DATA
- id of migrated persons, and PERSON_MIGR_ERRORS
- id of records with person thath didn't pass validation during migration.
I think, that the NOT EXIST is the bottleneck. But how could make this query so that it would be faster ?