我正在使用以下查询将人员从视图迁移到我的数据库,这太慢了:
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)
在连接的字段上有索引,但是我必须更改此查询,因为它持续15分钟。.表很大。
所以。我有PERSON_VIEW
-我从中读取要迁移的人员,PERSON_MIGR_DATA
-已迁移人员的ID,还有PERSON_MIGR_ERRORS
-与在迁移期间未通过验证的人员的记录ID。>
我认为,不存在是瓶颈。但是如何进行查询以使其更快呢?
答案 0 :(得分:4)
您可以尝试使用一对左连接来匹配(而不是不存在)
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
LEFT JOIN PERSON_MIGR_DATA PMD ON PMD.RECORD_ID = PERV.RECORD_ID
LEFT JOIN PERSON_MIGR_ERRORS PME ON PME.RECORD_ID = PERV.RECORD_ID
where PMD.RECORD_ID is null and PME.RECORD_ID is null