隨筆-295  評論-26  文章-1  trackbacks-0
          select S_10994_1_SYS_MODELTYPE.nextval,a.typeid,'SYS','Mail_Forward','郵件轉發模板','Mail Forward Model' from
          bse_organization a where not exists (select orgtypeid from SYS_MODELTYPE b where b.orgtypeid=a.typeid and modelcode ='Mail_Forward')
          --

          select S_10994_1_SYS_MODELTYPE.nextval,typeid,'SYS','Mail_Forward','郵件轉發模板','Mail Forward Model' from
          bse_organization where typeid not in (
          select orgtypeid from SYS_MODELTYPE where modelcode='Mail_Forward'
          )

          請注意not in 邏輯上不完全等同于not exists,如果你誤用了not in,小心你的程序存在致命的BUG:


          請看下面的例子:
          create table t1 (c1 number,c2 number);
          create table t2 (c1 number,c2 number);

          insert into t1 values (1,2);
          insert into t1 values (1,3);
          insert into t2 values (1,2);
          insert into t2 values (1,null);

          select * from t1 where c2 not in (select c2 from t2);
          no rows found
          select * from t1 where not exists (select 1 from t2 where t1.c2=t2.c2);
          c1 c2
          1 3

          正如所看到的,not in 出現了不期望的結果集,存在邏輯錯誤。如果看一下上述兩個select語句的執行計劃,也會不同。后者使用了hash_aj。
          因此,請盡量不要使用not in(它會調用子查詢),而盡量使用not exists(它會調用關聯子查詢)。如果子查詢中返回的任意一條記錄含有空值,則查詢將不返回任何記錄,正如上面例子所示。
          除非子查詢字段有非空限制,這時可以使用not in ,并且也可以通過提示讓它使用hasg_aj或merge_aj連接。



          大盤預測 國富論
          posted on 2007-10-11 16:51 華夢行 閱讀(194) 評論(0)  編輯  收藏 所屬分類: Oracle
          主站蜘蛛池模板: 偃师市| 阜平县| 龙里县| 囊谦县| 韶关市| 阳原县| 彰化县| 古丈县| 宁安市| 边坝县| 棋牌| 白山市| 汕尾市| 青铜峡市| 广东省| 平谷区| 曲周县| 黄浦区| 嫩江县| 青冈县| 冕宁县| 民丰县| 子洲县| 黔南| 琼结县| 工布江达县| 南木林县| 南江县| 阿城市| 肇州县| 锦屏县| 桐庐县| 壶关县| 开江县| 西城区| 古田县| 江门市| 岐山县| 海淀区| 鄂托克前旗| 隆回县|