Here is a simplified version of the actual query:
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
SELECT M.CNTR_NO FROM TB_GATE_TRAN GT,TB_MASTER M WHERE GT.VSL_CD = M.VSL_CD AND GT.CNTR_NO = M.CNTR_NO AND GT.CNTR_NO NOT IN (SELECT CNTR_NO FROM VT_APPT WHERE APPT_MOVE_TYPE = 'EmptyIn' AND STATUS IN ('Act','Mis') AND TRUCK_CO IN ('QTPI') AND APPT_MAKE_TIME > (trunc(SYSDATE) - 104) );
VT_APPT(APPT_MOVE_TYPE) VT_APPT(STATUS) VT_APPT(APPT_MOVE_TYPE,status) VT_APPT(CNTR_NO)
The problem: With the subquery, the SQL takes a lot of time, about 190 secs to return 520 rows. If we remove the subquery, it returns in 1 or 2 seconds. How can we speed up the query?
SELECT M.CNTR_NO FROM TB_GATE_TRAN GT,TB_MASTER M WHERE GT.VSL_CD = M.VSL_CD AND GT.CNTR_NO = M.CNTR_NO AND NOT EXISTS (SELECT 1 FROM VT_APPT VT WHERE VT.CNTR_NO = GT.CNTR_NO AND VT.APPT_MOVE_TYPE = 'EmptyIn' AND VT.STATUS IN ('Act','Mis') AND VT.TRUCK_CO IN ('QTPI') AND VT.APPT_MAKE_TIME > (trunc(SYSDATE) - 104));You could also use an antijoin of VT_APPT and TB_GATE_TRAN (where VT.CNTR_NO != GT.CNTR_NO), or create an inline view of your subquery. The point is to try the various ways that SQL allows you to express a query. You will frequently find that one approach is orders of magnitude faster than others.
Dig Deeper on Oracle database performance problems and tuning
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.