mysql 相反結果集查詢


今天閑來無事,開始整理數據庫記錄。想查詢一張表中沒有被另一張表引用的記錄,然后刪除之。上網查詢的方法為:使用not exists或not in.

參考文章:http://wenku.baidu.com/view/fe5f5e0d76c66137ee0619ab.html

     http://hnwsha.blog.sohu.com/177433557.html

文章都總結說,無論大表小表,not exists都比not in 效率高.

但是我的測試結果如下:

A為大表,B為小表。差距為A表記錄172683,B表記錄84.

select count(*) from A;

0.031 sec / 0.000 sec

select count(*) from B;

0.000 sec / 0.000 sec

select * from B where not exists (select b from A
where a = 2 and b = B.b);

4.485 sec / 0.000 sec

select * from B where b not in (select b from A
where a = 2);

4.281 sec / 0.000 sec

結果分析:如果主查詢為小表,且與子查詢中的大表記錄差距很大時,反而是not in 效率更高。但是要注意子查詢中沒有NULL值,否則沒有返回集。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM