系統要求進行sql優化,對效率比較低的sql進行優化,使其執行效率更高,其中要求對sql中的部分in/not in修改為exists/not exists
修改方法如下:
in的sql語句
select id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
from tab_oa_pub where is_check=1 and
category_id in (select id from tab_oa_pub_cate where no='1')
order by begintime desc
修改為exists的sql語句
select id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
from tab_oa_pub where is_check=1 and
exists (select id from tab_oa_pub_cate where tab_oa_pub.category_id=convert(int,no) and no='1')
order by begintime desc
分析一下exists真的就比in的效率高嗎?
我們要根據實際的情況做相應的優化,不能絕對的說誰的效率高誰的效率低,所有的事都是相對的
in和exists的效率問題
in 是把外表和內錶作hash鏈結 exists是對外表作loop迴圈,每次loop迴圈再對內表進行查詢。一直以來總認為exists比in的效率高,這種說法是不準確的。如果查詢的兩個表大小相當的話,那麼用in和exists的效率差別不大。如果兩個表中乙個較小的表a,乙個大表b,兩個表都有字段cc 則...
單引號真的比雙引號更具效率嗎?
測試環境 centos5.4 測試工具 webbench 測試 echo hello world echo hello world 測試命令 webbench c 5000 t 30 測試結果 雙引號 speed 8394 pages min,282969 bytes sec.單引號 speed 7...
關於EXISTS的使用及效率
本文參考了不過的oracle部落格http www.cnblogs.com yf520gn archive 2009 01 12 1374359.html 後根據自己的理解來寫的。建立兩張表t1 t2,其中t1的locline列中的某些值存在於t2的location中 create table t1...