Oracle查询优化中使用EXISTS可以比使用IN更加速度,当然这只能是在只有一个IN 的情况下,一般有多个IN的情况下,不能将所有的IN都换成EXISTS,道理我也不清楚,我只是根据实际情况的出来的结论。
采用EXISTS+IN的模式:
SELECT * FROM EXP_REPORT_HEADERS ERH WHERE EXISTS (SELECT 1 FROM EXP_REPORT_LINES ERL WHERE ERL.EXP_REPORT_HEADER_ID = ERH.EXP_REPORT_HEADER_ID AND ERL.CREATION_DATE >= TO_DATE(‘2014-01-01‘, ‘yyyy-mm-dd‘)) /* AND EXISTS (SELECT 1 FROM FND_COMPANIES FC WHERE FC.SET_OF_BOOKS_ID = 33 AND ERH.COMPANY_ID = FC.COMPANY_ID)*/ AND ERH.COMPANY_ID IN (SELECT FC.COMPANY_ID FROM FND_COMPANIES FC WHERE FC.SET_OF_BOOKS_ID = 33)
耗时:0:01
采用EXISTS+EXISTS
SELECT * FROM EXP_REPORT_HEADERS ERH WHERE EXISTS (SELECT 1 FROM EXP_REPORT_LINES ERL WHERE ERL.EXP_REPORT_HEADER_ID = ERH.EXP_REPORT_HEADER_ID AND ERL.CREATION_DATE >= TO_DATE(‘2014-01-01‘, ‘yyyy-mm-dd‘)) AND EXISTS (SELECT 1 FROM FND_COMPANIES FC WHERE FC.SET_OF_BOOKS_ID = 33 AND ERH.COMPANY_ID = FC.COMPANY_ID)/* AND ERH.COMPANY_ID IN (SELECT FC.COMPANY_ID FROM FND_COMPANIES FC WHERE FC.SET_OF_BOOKS_ID = 33) */
耗时:0:11