ORACLE SQL效率 實踐

來源:互聯網
上載者:User

重點關注9:用EXISTS替代IN.重點關注

案例 :尋找DEPT_1部門的人員姓名

9.1 SQL語句

--子查詢select pname from t_person where deptid in (       select deptid from t_department where deptname like 'DEPT_1');--exsistselect pname from t_person where exists (       select * from t_department where t_department.deptid = t_person.deptid       and deptname like 'DEPT_1');--外串連
select pname
  from t_person p LEFT join t_department d ON p.deptid = d.deptid
 WHERE d.deptname like 'DEPT_1';

9.2 測試代碼塊

--子查詢代碼塊測試  declare        v_sql  varchar2(300);        v_diff number;      begin        v_sql := 'select pname from t_person where deptid in (         select deptid from t_department where deptname like ''DEPT_1'') ';        select F_TEST_TIME_efficiency(v_sql) into v_diff from dual;      end;  --exists代碼塊測試    declare      v_sql  varchar2(300);      v_diff number;    begin      v_sql := 'select pname from t_person where exists (       select * from t_department where t_department.deptid = t_person.deptid       and deptname like ''DEPT_1'')';      select F_TEST_TIME_efficiency(v_sql) into v_diff from dual;    end; --外串連代碼塊測試    declare      v_sql  varchar2(300);      v_diff number;    begin      v_sql := 'select pname  from t_person p LEFT join t_department d ON p.deptid = d.deptid WHERE d.deptname like ''DEPT_1''';      select F_TEST_TIME_efficiency(v_sql) into v_diff from dual;    end;

9.3 輸出結果

Time differences is  10Time differences is  9Time differences is  10

9.4

效率由高到低:exists> 外串連=子查詢

重點關注10:用NOT EXISTS替代NOT IN .

案例 :尋找 非DEPT_1部門的 人員姓名

10.1 SQL語句

 --子查詢 SELECT  p.pnameFROM T_PERSONWHERE DEPTID NOT IN (SELECT DEPTIDFROM T_DEPARTMENTWHERE DEPTNAME LIKE 'DEPT_1'); --外串連 查詢結果包含沒有部門的人員 select p.pname  from t_person p left join t_department d on p.deptid = d.deptid    where d.DEPTNAME  not like 'DEPT_1' or d.DEPTNAME is null;--exists法  查詢結果不包含沒有部門的人員select p.pname  from t_person p where exists ( select * from t_department  d where  p.deptid = d.deptid and d.DEPTNAME  not like 'DEPT_1'  );

10.2 執行代碼塊(在每個測試之前都要清空資料庫緩衝)

 

--子查詢代碼塊測試declare      v_sql  varchar2(300);      v_diff number;    begin      v_sql := 'SELECT T_PERSON.pname    FROM T_PERSON    WHERE DEPTID NOT IN (SELECT DEPTID    FROM T_DEPARTMENT    WHERE DEPTNAME LIKE ''DEPT_1'')';      select F_TEST_TIME_efficiency(v_sql) into v_diff from dual;    end;  --外串連代碼塊測試    declare      v_sql  varchar2(300);      v_diff number;    begin      v_sql := 'select p.pname      from t_person p left join t_department d     on p.deptid = d.deptid        where d.DEPTNAME  not like ''DEPT_1'' or d.DEPTNAME is null';      select F_TEST_TIME_efficiency(v_sql) into v_diff from dual;    end; --exists代碼塊測試    declare      v_sql  varchar2(300);      v_diff number;    begin      v_sql := 'select p.pname      from t_person p     where exists (     select * from t_department  d where  p.deptid = d.deptid and d.DEPTNAME  not like ''DEPT_1'')';      select F_TEST_TIME_efficiency(v_sql) into v_diff from dual;    end; 

10.3 清空oracle緩衝的語句

ALTER SYSTEM FLUSH BUFFER_CACHE;  ALTER SYSTEM FLUSH SHARED_POOL ;ALTER SYSTEM FLUSH GLOBAL CONTEXT;ALTER SYSTEM SET EVENTS='IMMEDIATE TRACE NAME FLUSH_CACHE'; 

10.4 輸出結果

Time differences is  9

Time differences is  8

Time differences is  8

10.5 結論

效率由高到低:外串連=exists >子查詢

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.