[20170606]11G _optimizer_null_aware_antijoin.txt

--//上午测试_optimizer_null_aware_antijoin,发现自己不经意间又犯了一个低级错误,做1个记录.

1.环境:
SYS@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SYS@book> @ &r/hide _optimizer_null_aware_antijoin
NAME                           DESCRIPTION                   DEFAULT_VALUE SESSION_VALUE SYSTEM_VALUE
------------------------------ ----------------------------- ------------- ------------- ------------
_optimizer_null_aware_antijoin null-aware antijoin parameter TRUE          TRUE          TRUE

2.做一个测试例子:
SCOTT@book> create table t1 as select * from dba_objects;
Table created.

SCOTT@book> create table t2 as select * from dba_objects;
Table created.

SCOTT@book> select count(*) from t2;
  COUNT(*)
----------
     86996

SCOTT@book> select count(*) from t1;
  COUNT(*)
----------
     86995
--//分析表略.
--//因为T1,T2建立有先后,这样T2的记录数量比T1记录的数量多1.注意1个特点允许NULL(这样建立的表),如果你使用all_objects来建立就情况不同了:
--//你可以查看定义desc all_objects, desc dba_objects.
SCOTT@book> @ &r/desc t1
   Name            Null? Type
   --------------- ----- --------------
 1 OWNER                 VARCHAR2(30)
 2 OBJECT_NAME           VARCHAR2(128)
 3 SUBOBJECT_NAME        VARCHAR2(30)
 4 OBJECT_ID             NUMBER
 5 DATA_OBJECT_ID        NUMBER
 6 OBJECT_TYPE           VARCHAR2(19)
 7 CREATED               DATE
 8 LAST_DDL_TIME         DATE
 9 TIMESTAMP             VARCHAR2(19)
10 STATUS                VARCHAR2(7)
11 TEMPORARY             VARCHAR2(1)
12 GENERATED             VARCHAR2(1)
13 SECONDARY             VARCHAR2(1)
14 NAMESPACE             NUMBER
15 EDITION_NAME          VARCHAR2(30)

SCOTT@book> select * from t1 where object_name='T2';
no rows selected

SCOTT@book> select * from t2 where object_name='T2';
OWNER  OBJECT_NAME SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ ----------- ---------- ---------- -------------- ----------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
SCOTT  T2                          90863          90863 TABLE       2017-06-06 16:12:27 2017-06-06 16:12:27 2017-06-06:16:12:27 VALID   N N N          1

SCOTT@book> select  * from t1 where OBJECT_ID=90863;
no rows selected

3.测试:
SCOTT@book> alter session set statistics_level=all;
Session altered.

SCOTT@book> select count(*) from t2 where object_id not in(select object_id from t1);
  COUNT(*)
----------
         0

SCOTT@book> select * from t2 where object_id not in(select object_id from t1);
no rows selected
--//why? 理论应该返回1行啊.

SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  45ppus55apd1z, child number 1
-------------------------------------
select * from t2 where object_id not in(select object_id from t1)
Plan hash value: 35395643
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation               | Name | Starts | E-Rows |E-Bytes|E-Temp | Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |      |      1 |        |       |       |  1218 (100)|          |      0 |00:00:00.06 |    1244 |   1242 |       |       |          |
|*  1 |  HASH JOIN RIGHT ANTI NA|      |      1 |    870 | 89610 |  1448K|  1218   (1)| 00:00:15 |      0 |00:00:00.06 |    1244 |   1242 |  1696K|  1696K| 2503K (0)|
|   2 |   TABLE ACCESS FULL     | T1   |      1 |  86995 |   424K|       |   347   (1)| 00:00:05 |  86994 |00:00:00.02 |    1244 |   1242 |       |       |          |
|   3 |   TABLE ACCESS FULL     | T2   |      0 |  86996 |  8325K|       |   347   (1)| 00:00:05 |      0 |00:00:00.01 |       0 |      0 |       |       |          |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$5DA710D3
   2 - SEL$5DA710D3 / T1@SEL$2
   3 - SEL$5DA710D3 / T2@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("OBJECT_ID"="OBJECT_ID")
27 rows selected.

--//这才想起来以前的一个错误,不能这样查询,因为IN中的记录存在NULL,这样NULL<>NULL.应该改写如下:

SCOTT@book> select * from t2 where object_id not in(select object_id from t1 where t1.object_id is not null );
OWNER  OBJECT_NAME          SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ -------------------- ---------- ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
SCOTT  T2                                   90863          90863 TABLE               2017-06-06 16:12:27 2017-06-06 16:12:27 2017-06-06:16:12:27 VALID   N N N          1

SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  9cn6amuwk12r4, child number 0
-------------------------------------
select * from t2 where object_id not in(select object_id from t1 where
t1.object_id is not null )
Plan hash value: 2323664790
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                | Name | Starts | E-Rows |E-Bytes|E-Temp | Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT         |      |      1 |        |       |       |  1218 (100)|          |      1 |00:00:00.15 |    2489 |   2484 |       |       |          |
|*  1 |  HASH JOIN RIGHT ANTI SNA|      |      1 |    870 | 89610 |  1448K|  1218   (1)| 00:00:15 |      1 |00:00:00.15 |    2489 |   2484 |  5416K|  2890K| 5574K (0)|
|*  2 |   TABLE ACCESS FULL      | T1   |      1 |  86993 |   424K|       |   347   (1)| 00:00:05 |  86993 |00:00:00.02 |    1244 |   1242 |       |       |          |
|   3 |   TABLE ACCESS FULL      | T2   |      1 |  86996 |  8325K|       |   347   (1)| 00:00:05 |  86996 |00:00:00.03 |    1245 |   1242 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$5DA710D3
   2 - SEL$5DA710D3 / T1@SEL$2
   3 - SEL$5DA710D3 / T2@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("OBJECT_ID"="OBJECT_ID")
   2 - filter("T1"."OBJECT_ID" IS NOT NULL)
--//注意执行计划中的HASH JOIN RIGHT ANTI SNA.

4.测试"_optimizer_null_aware_antijoin"=false看看.

SCOTT@book> alter session set "_optimizer_null_aware_antijoin"=false;
Session altered.

SCOTT@book> Select * from t2 where object_id not in(select object_id from t1 where t1.object_id is not null );
OWNER  OBJECT_NAME          SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ -------------------- ---------- ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
SCOTT  T2                                   90863          90863 TABLE               2017-06-06 16:12:27 2017-06-06 16:12:27 2017-06-06:16:12:27 VALID   N N N          1
--//昏,那个慢受不了.......

SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4ps64yjz4g7pu, child number 0
-------------------------------------
Select * from t2 where object_id not in(select object_id from t1 where
t1.object_id is not null )
Plan hash value: 2132554994
------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation          | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |
------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |      1 |        |       |  8847K(100)|          |      1 |00:04:06.56 |      53M|     67M|
|*  1 |  FILTER            |      |      1 |        |       |            |          |      1 |00:04:06.56 |      53M|     67M|
|   2 |   TABLE ACCESS FULL| T2   |      1 |  86996 |  8325K|   347   (1)| 00:00:05 |  86996 |00:00:00.05 |    1245 |   1242 |
|*  3 |   TABLE ACCESS FULL| T1   |  86995 |      2 |    10 |   116   (0)| 00:00:02 |  86994 |00:04:05.95 |      53M|     67M|
------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1
   2 - SEL$1 / T2@SEL$1
   3 - SEL$2 / T1@SEL$2
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter( IS NULL)
   3 - filter(("T1"."OBJECT_ID" IS NOT NULL AND LNNVL("OBJECT_ID"<>:B1)))
29 rows selected.

3.换1个方式:

SCOTT@book> create table t11 as select * from all_objects;
Table created.

SCOTT@book> create table t22 as select * from all_objects;
Table created.
--//分析表略.

SCOTT@book> @ &r/desc t11
Name           Null?    Type
-------------- -------- ------------
OWNER          NOT NULL VARCHAR2(30)
OBJECT_NAME    NOT NULL VARCHAR2(30)
SUBOBJECT_NAME          VARCHAR2(30)
OBJECT_ID      NOT NULL NUMBER
DATA_OBJECT_ID          NUMBER
OBJECT_TYPE             VARCHAR2(19)
CREATED        NOT NULL DATE
LAST_DDL_TIME  NOT NULL DATE
TIMESTAMP               VARCHAR2(19)
STATUS                  VARCHAR2(7)
TEMPORARY               VARCHAR2(1)
GENERATED               VARCHAR2(1)
SECONDARY               VARCHAR2(1)
NAMESPACE      NOT NULL NUMBER
EDITION_NAME            VARCHAR2(30)

--// 注意这样定义的表OBJECT_ID是非空.

SCOTT@book> select * from t22 where object_id not in(select object_id from t11);
OWNER  OBJECT_NAME          SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ -------------------- ---------- ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
SCOTT  T22                                  90869          90869 TABLE               2017-06-06 16:33:29 2017-06-06 16:33:29 2017-06-06:16:33:29 VALID   N N N          1

SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  31yq53mnd3g7k, child number 1
-------------------------------------
select * from t22 where object_id not in(select object_id from t11)
Plan hash value: 3941821364
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation            | Name | Starts | E-Rows |E-Bytes|E-Temp | Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |      1 |        |       |       |  1187 (100)|          |      1 |00:00:00.15 |    2427 |   2422 |       |       |          |
|*  1 |  HASH JOIN RIGHT ANTI|      |      1 |    848 | 87344 |  1408K|  1187   (1)| 00:00:15 |      1 |00:00:00.15 |    2427 |   2422 |  5236K|  2890K| 5538K (0)|
|   2 |   TABLE ACCESS FULL  | T11  |      1 |  84763 |   413K|       |   338   (1)| 00:00:05 |  84763 |00:00:00.02 |    1213 |   1211 |       |       |          |
|   3 |   TABLE ACCESS FULL  | T22  |      1 |  84764 |  8112K|       |   338   (1)| 00:00:05 |  84764 |00:00:00.03 |    1214 |   1211 |       |       |          |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$5DA710D3
   2 - SEL$5DA710D3 / T11@SEL$2
   3 - SEL$5DA710D3 / T22@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("OBJECT_ID"="OBJECT_ID")

--//注意执行计划HASH JOIN RIGHT ANTI.我想起来以前10g我也遇到类型问题,将数据类型修改为NULL.

4.退出测试使用not exists的情况:

SCOTT@book> select * from t2 where not exists  (select 1 from t1  where t1.OBJECT_ID=t2.object_id);
OWNER  OBJECT_NAME SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ ----------- ---------- ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
SCOTT  T2                          90863          90863 TABLE               2017-06-06 16:12:27 2017-06-06 16:12:27 2017-06-06:16:12:27 VALID   N N N          1
PUBLIC LOOPBACK                                         DATABASE LINK       2016-12-14 15:17:00                                         VALID   N N N
PUBLIC TEST040                                          DATABASE LINK       2017-01-09 09:14:26                                         VALID   N N N
--//这样返回3条.也就是这样查询注意空值的情况.也就是要加入t2.object_id is not null 条件,才会返回1条.

SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4jx0g5ndct9vk, child number 0
-------------------------------------
select * from t2 where not exists  (select 1 from t1  where
t1.OBJECT_ID=t2.object_id)
Plan hash value: 1142061071
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation            | Name | Starts | E-Rows |E-Bytes|E-Temp | Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |      1 |        |       |       |  1218 (100)|          |      3 |00:00:00.14 |    2489 |   2484 |       |       |          |
|*  1 |  HASH JOIN RIGHT ANTI|      |      1 |    870 | 89610 |  1448K|  1218   (1)| 00:00:15 |      3 |00:00:00.14 |    2489 |   2484 |  5416K|  2890K| 5759K (0)|
|   2 |   TABLE ACCESS FULL  | T1   |      1 |  86995 |   424K|       |   347   (1)| 00:00:05 |  86995 |00:00:00.02 |    1244 |   1242 |       |       |          |
|   3 |   TABLE ACCESS FULL  | T2   |      1 |  86996 |  8325K|       |   347   (1)| 00:00:05 |  86996 |00:00:00.02 |    1245 |   1242 |       |       |          |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$5DA710D3
   2 - SEL$5DA710D3 / T1@SEL$2
   3 - SEL$5DA710D3 / T2@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")

SCOTT@book> select * from t1 where object_id is null ;
OWNER  OBJECT_NAME          SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ -------------------- ---------- ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
PUBLIC LOOPBACK                                                  DATABASE LINK       2016-12-14 15:17:00                                         VALID   N N N
PUBLIC TEST040                                                   DATABASE LINK       2017-01-09 09:14:26                                         VALID   N N N
--//正好2条object_id is null.

5.总结:
1.再次注意not in 与NULL的查询.
2.建立良好的约束,规避一些这样的问题.

[20170606]11G _optimizer_null_aware_antijoin.txt的更多相关文章

  1. 【Oracle】跟踪文件目录(User_Dump_Dest)生成脚本(11g/9i).txt

    oracle 11g 生成跟踪目录(User_Dump_Dest)脚本: –设置合适显示长度: column trace new_val TRACE format a100 –脚本: select c ...

  2. 11g手动打补丁

    在两个节点上,分别以oracle用户执行,停止资源  节点一: srvctl stop home -o /app/oracle/11g -s /home/oracle/s1 -n 11grac75 节 ...

  3. 转://从一条巨慢SQL看基于Oracle的SQL优化

    http://mp.weixin.qq.com/s/DkIPwbDKIjH2FMN13GkT4w 本次分享的内容是基于Oracle的SQL优化,以一条巨慢的SQL为例,从快速解读SQL执行计划.如何从 ...

  4. 从一条巨慢SQL看基于Oracle的SQL优化(重磅彩蛋+PPT)

    本文根据DBAplus社群第110期线上分享整理而成,文末还有好书送哦~ 讲师介绍 丁俊 新炬网络首席性能优化专家 SQL审核产品经理 DBAplus社群联合发起人.<剑破冰山-Oracle开发 ...

  5. Linux内核配置选项

    http://blog.csdn.net/wdsfup/article/details/52302142 http://www.manew.com/blog-166674-12962.html Gen ...

  6. [20190416]11g下那些latch是Exclusive的.txt

    [20190416]11g下那些latch是Exclusive的.txt --//昨天测试了11g下那些latch是共享的,链接:--//是否反过来剩下的都是Exclusive的.继续测试: 1.环境 ...

  7. [20190415]11g下那些latch是共享的.txt

    [20190415]11g下那些latch是共享的.txt http://andreynikolaev.wordpress.com/2010/11/23/shared-latches-by-oracl ...

  8. [20190219]那个更快(11g).txt

    [20190219]那个更快(11g).txt --//前几天测试11g Query Result Cache RC Latches时,链接http://blog.itpub.net/267265/v ...

  9. [20190214]11g Query Result Cache RC Latches补充.txt

    [20190214]11g Query Result Cache RC Latches补充.txt --//上午测试链接:http://blog.itpub.net/267265/viewspace- ...

随机推荐

  1. Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框

    作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]h ...

  2. How Tomcat works — 一、怎样阅读源码

    在编程的道路上,通过阅读优秀的代码来提升自己是很好的办法.一直想阅读一些开源项目,可是没有合适的机会开始.最近做项目的时候用到了shiro,需要做集群的session共享,经过查找发现tomcat的s ...

  3. Unicode 是不是只有两个字节,为什么能表示超过 65536 个字符

      Unicode 目前规划的总空间是17个平面(平面0至16),0x0000 至 0x10FFFF.每个平面有 65536 个码点.你只是大致知道平面0(「Basic Multilingual Pl ...

  4. You must reset your password using ALTER USER statement before executing this statement.

    MySQL 5.7之后,刚初始化的MySQL实例要求先修改密码.否则会报错: mysql> create database test; ERROR 1820 (HY000): You must ...

  5. vue 路由 及 跳转传递参数的总结

    博客地址:https://ainyi.com/4 vue-router vue-router 是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基 ...

  6. 【转】探讨:ASP.NET技术的学习顺序问题

    摘要:很多人对于ASP.NET的入门和学习顺序比较迷茫,今天让我们一起来跟随作者的思路学习探讨ASP.NET的学习顺序问题,希望有所帮助. 如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 ...

  7. [转]Rabbitmq的使用及Web监控工具使用

    本文转自:https://blog.csdn.net/xingxing513234072/article/details/51014850 一.文档资料        1.官方网站:http://ww ...

  8. DataTable与List<T>相互转换

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. Laravel 系列入门教程(三)【最适合中国人的 Laravel 教程】

    在本篇文章中,我们将尝试构建一个带后台的简单博客系统.我们将会使用到 路由.MVC.Eloquent ORM 和 blade 视图系统. 简单博客系统规划 我们在教程一中已经新建了一个继承自 Eloq ...

  10. C# Dev XtraReport 简单测试

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpre ...