posts - 167,  comments - 30,  trackbacks - 0
          IN
          確定給定的值是否與子查詢或列表中的值相匹配。

          EXISTS
          指定一個子查詢,檢測行的存在。

          比較使用 EXISTS 和 IN 的查詢

          這個例子比較了兩個語義類似的查詢。
          第一個查詢使用 EXISTS 而第二個查詢使用 IN。注意兩個查詢返回相同的信息。
          SELECT DISTINCT pub_name
          FROM publishers
          WHERE EXISTS
          (SELECT *
          FROM titles
          WHERE pub_id = publishers.pub_id
          AND type = 'business')

          -- Or, using the IN clause:

          SELECT distinct pub_name
          FROM publishers
          WHERE pub_id IN
          (SELECT pub_id
          FROM titles
          WHERE type = 'business')

          下面是任一查詢的結果集:
          pub_name
          ----------------------------------------
          Algodata Infosystems
          New Moon Books


          exits 相當于存在量詞:表示集合存在,也就是集合不為空只作用一個集合.
          例如 exist P表示P不空時為真; not exist P表示p為空時為真in表示一個標量和一元關系的關系。
          例如:s in P表示當s與P中的某個值相等時 為真; s not in P 表示s與P中的每一個值都不相等時 為真

          在Oracle SQL中取數據時有時要用到in 和 exists 那么他們有什么區別呢?

          1)性能上的比較
          比如Select * from T1 where x in ( select y from T2 )
          執行的過程相當于:
          select *
            from t1, ( select distinct y from t2 ) t2
          where t1.x = t2.y;

          相對的

          select * from t1 where exists ( select null from t2 where y = x )
          執行的過程相當于:
          for x in ( select * from t1 )
             loop
                if ( exists ( select null from t2 where y = x.x )
                then
                   OUTPUT THE RECORD
                end if
          end loop
          表 T1 不可避免的要被完全掃描一遍

          分別適用在什么情況?
          以子查詢 ( select y from T2 )為考慮方向
          如果子查詢的結果集很大需要消耗很多時間,但是T1比較小執行( select null from t2 where y = x.x )非常快,那么exists就比較適合用在這里
          相對應得子查詢的結果集比較小的時候就應該使用in.

          in和exists
          in 是把外表和內表作hash 連接,而exists是對外表作loop循環,每次loop循環再對內表進行查詢。
          一直以來認為exists比in效率高的說法是不準確的。
          如果查詢的兩個表大小相當,那么用in和exists差別不大。

          如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:
          例如:表A(小表),表B(大表)
          1:
          select * from A where cc in (select cc from B)
          效率低,用到了A表上cc列的索引;
          select * from A where exists(select cc from B where cc=A.cc)
          效率高,用到了B表上cc列的索引。

          相反的
          2:
          select * from B where cc in (select cc from A)
          效率高,用到了B表上cc列的索引;
          select * from B where exists(select cc from A where cc=B.cc)
          效率低,用到了A表上cc列的索引。

          not in 和not exists
          如果查詢語句使用了not in 那么內外表都進行全表掃描,沒有用到索引;
          而not extsts 的子查詢依然能用到表上的索引。
          所以無論那個表大,用not exists都比not in要快。

          in 與 =的區別
          select name from student where name in ('zhang','wang','li','zhao');


          select name from student where name='zhang' or name='li' or name='wang' or name='zhao'的結果是相同的。
          posted on 2009-03-31 13:08 David1228 閱讀(4354) 評論(0)  編輯  收藏 所屬分類: 數據庫

          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章檔案

          新聞分類

          新聞檔案

          相冊

          收藏夾

          Java

          Linux知識相關

          Spring相關

          云計算/Linux/虛擬化技術/

          友情博客

          多線程并發編程

          開源技術

          持久層技術相關

          搜索

          •  

          積分與排名

          • 積分 - 359318
          • 排名 - 154

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 太原市| 衡水市| 石屏县| 麦盖提县| 阿瓦提县| 山阴县| 措勤县| 平阳县| 稻城县| 屏山县| 连城县| 揭东县| 垫江县| 久治县| 盐池县| 西乡县| 乐山市| 涡阳县| 鄂尔多斯市| 五家渠市| 苍梧县| 哈尔滨市| 浑源县| 宣汉县| 诸城市| 罗城| 江油市| 抚顺县| 水富县| 德惠市| 道孚县| 朝阳县| 长沙县| 祁连县| 慈利县| 松潘县| 琼海市| 平武县| 江门市| 纳雍县| 吴川市|