SELECT vbeln,kunnr,erdat FROM ( SELECT k.vbeln,k.kunnr,k.erdat,(SELECT temp.vbeln FROM sap_vbak temp WHERE temp.erdat < k.erdat and temp.erdat >= k.erdat -45and temp.kunnr = k.kunnr and rownum =1) tmp FROM sap_vbak k WHERE k.erdat >= to_date('2018-01-01','yyyy-MM-dd') and k.erdat <= to_date('2018-01-05','yyyy-MM-dd') groupby k.vbeln,k.kunnr,k.erdat ) WHERE tmp isnull;
SELECT k.vbeln,k.kunnr,k.erdat FROM sap_vbak k LEFTJOIN sap_vbak temp ON temp.erdat < k.erdat and temp.erdat >= k.erdat -45and temp.kunnr = k.kunnr WHERE k.erdat >= to_date('2018-01-01','yyyy-MM-dd') and k.erdat <= to_date('2018-01-15','yyyy-MM-dd') and temp.vbeln isnull;
SELECT k.vbeln,k.kunnr,k.erdat FROM sap_vbak k WHERE k.erdat >= to_date('2018-01-01','yyyy-MM-dd') and k.erdat <= to_date('2018-01-05','yyyy-MM-dd') andnotexists (SELECT temp.vbeln FROM sap_vbak temp WHERE temp.erdat < k.erdat and temp.erdat >= k.erdat -45and temp.kunnr = k.kunnr);
把原先的标量子查询改成了WHERE后面的 not exists ,查询时间也从原本的15分钟,变到了5分钟以内。