隨筆-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 華夢行 閱讀(188) 評論(0)  編輯  收藏 所屬分類: Oracle
          主站蜘蛛池模板: 桂阳县| 台中市| 广汉市| 台湾省| 托克托县| 二手房| 中江县| 绵竹市| 北宁市| 象州县| 北票市| 孟州市| 永平县| 武乡县| 南部县| 柞水县| 焉耆| 郸城县| 天峻县| 彩票| 山东省| 武汉市| 阆中市| 石屏县| 师宗县| 奎屯市| 德钦县| 台江县| 胶州市| 长岛县| 二连浩特市| 大邑县| 巢湖市| 额敏县| 祁阳县| 吴旗县| 六安市| 马尔康县| 双城市| 福贡县| 襄汾县|