本文从不绑定变量和绑定变量两种情况讨论直方图的作用

一、不绑定变量

SQL> create table test(name varchar2(10));
表已创建。
SQL> insert into test select 'A' from table1;
已创建25064行。
SQL> insert into test values('B');
已创建 1 行。

SQL> insert into test values('C');
已创建 1 行。

SQL> select name,count(1) from test group by name;
NAME         COUNT(1)
---------- ----------
A               25064
B                   1
C                   1

SQL> create index i_test on test(name);
索引已创建。

SQL> analyze table test compute statistics;
表已分析。

SQL> select * from test where name='A';
已选择25064行。

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8355 |  8355 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |  8355 |  8355 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"='A')

统计信息
----------------------------------------------------------
         32  recursive calls
          0  db block gets
       1720  consistent gets
          0  physical reads
          0  redo size
     337843  bytes sent via SQL*Net to client
      18770  bytes received via SQL*Net from client
       1672  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      25064  rows processed

SQL> select * from test where name='B';

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8355 |  8355 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |  8355 |  8355 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"='B')

统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         47  consistent gets
          0  physical reads
          0  redo size
        407  bytes sent via SQL*Net to client
        400  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

只需返回一条数据,但做了全表扫描。

因为,oracle只知道name列有3个不同的值,但不知道每个不同的值分别有多少记录,oracle默认这些数据是完全均匀的,

所以,当用name做条件时,oracle认为会返回总记录的三分之一(从Rows=8355可以看出)

对test表生成直方图后再做同样的查询

SQL> analyze table test compute statistics for table for all indexes for all indexed columns;

表已分析。

SQL> select * from test where name='A';
已选择25064行。

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 25064 | 25064 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST | 25064 | 25064 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"='A')

统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       1717  consistent gets
          0  physical reads
          0  redo size
     337843  bytes sent via SQL*Net to client
      18770  bytes received via SQL*Net from client
       1672  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      25064  rows processed

SQL> select * from test where name='B';

执行计划
----------------------------------------------------------
Plan hash value: 3559141341
---------------------------------------------------------------------------
| Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT |        |     1 |     1 |     1   (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| I_TEST |     1 |     1 |     1   (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("NAME"='B')

统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          3  consistent gets
          0  physical reads
          0  redo size
        407  bytes sent via SQL*Net to client
        400  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

可见,生成了直方图后,oracle会根据数据的实际分布情况选择合适的执行计划。

###############################################################

二、绑定变量的情况下

SQL> analyze table test compute statistics;
表已分析。

SQL> var o varchar2(10)
SQL> exec :o:='A'
PL/SQL 过程已成功完成。

SQL> select * from test where name=:o;
已选择25064行。

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8355 |  8355 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |  8355 |  8355 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=:O)

统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       1717  consistent gets
          0  physical reads
          0  redo size
     337843  bytes sent via SQL*Net to client
      18770  bytes received via SQL*Net from client
       1672  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      25064  rows processed

SQL> exec :o:='B'
PL/SQL 过程已成功完成。

SQL> select * from test where name=:o;

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8355 |  8355 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |  8355 |  8355 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=:O)

统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         47  consistent gets
          0  physical reads
          0  redo size
        407  bytes sent via SQL*Net to client
        400  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

从以上测试可以看出,在绑定变量的情况下,如果没有分析直方图,两个查询都使用了相同的执行计划——全表扫描。

在第一次解析SQL的时候,因为oracle不知道数据的具体分布,所以它认为会返回三分之一的数据,所以选择了全表扫描。

在以后执行同样的SQL时会重用该SQL,都会使用第一次解析生成的执行计划。

在本例中,无论:o是'A'还是'B',都会使用全表扫描,那么,我们是否可以得出这样一个结论:

如果分析了直方图,那么如果第一次硬解析SQL时:o是'A'时,会使用全表扫描;:o是'B'时,会使用索引扫描呢?看如下的测试:

SQL> alter system flush shared_pool;
系统已更改。
SQL> analyze table test delete statistics;
表已分析。
SQL> analyze table test compute statistics for table for all indexes for all indexed columns;
表已分析。
SQL> exec :o:='A'
PL/SQL 过程已成功完成。
SQL> select * from test where name=:o;
已选择25064行。

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8355 |  8355 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |  8355 |  8355 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=:O)

统计信息
----------------------------------------------------------
         32  recursive calls
          0  db block gets
       1720  consistent gets
          0  physical reads
          0  redo size
     337843  bytes sent via SQL*Net to client
      18770  bytes received via SQL*Net from client
       1672  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      25064  rows processed

SQL> alter system flush shared_pool;
系统已更改。
SQL> analyze table test delete statistics;
表已分析。
SQL> analyze table test compute statistics for table for all indexes for all indexed columns;
表已分析。
SQL> exec :o:='B'
PL/SQL 过程已成功完成。

SQL> select * from test where name=:o;

执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8355 |  8355 |    14   (8)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TEST |  8355 |  8355 |    14   (8)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=:O)

统计信息
----------------------------------------------------------
         32  recursive calls
          0  db block gets
          6  consistent gets
          0  physical reads
          0  redo size
        407  bytes sent via SQL*Net to client
        400  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

从这个结果可以看出,分析了直方图后,无论:o的值是'A'还是'B',第一次执行该sql时,使用的都是全表扫描,这与刚才的推论不一致了。

如果真是这样的话,使用绑定变量对表做直方图还有什么意义呢?其实这应该算是oracl的一个bug,在这里autotrace的结果是不对的,我们可以用10046看

启用 Oracle 10046 调试事件

SQL> alter system flush shared_pool;
SQL> analyze table test delete statistics;
SQL> analyze table test compute statistics for table for all indexes for all indexed columns;
SQL> exec :o:='A'

SQL> ALTER SESSION SET EVENTS '10046 trace name context forever, level 12';

SQL> select * from test where name=:o;

SQL> ALTER SESSION SET EVENTS '10046 trace name context off';

将C:\oracle\product\10.2.0\admin\orcl\udump下的最新trc文件copy至桌面

C:\Users\LEE\Desktop> tkprof orcl_ora_4516.trc orcla.sql

查看orcla.sql

select * 
from
 test where name=:o

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        2      0.04       0.11          0          3          0           0
Execute      2      0.00       0.02          0          0          0           0
Fetch     1674      0.14       0.14          0       1720          0       25065
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total     1678      0.18       0.28          0       1723          0       25065

Misses in library cache during parse: 2
Optimizer mode: ALL_ROWS
Parsing user id: 58

Rows     Row Source Operation
-------  ---------------------------------------------------
  25064  TABLE ACCESS FULL TEST (cr=1717 pr=0 pw=0 time=100367 us)

——————————————————

SQL> alter system flush shared_pool;
SQL> analyze table test delete statistics;
SQL> analyze table test compute statistics for table for all indexes for all indexed columns;
SQL> exec :o:='B'

SQL> ALTER SESSION SET EVENTS '10046 trace name context forever, level 12';

SQL> select * from test where name=:o;

SQL> ALTER SESSION SET EVENTS '10046 trace name context off';

将C:\oracle\product\10.2.0\admin\orcl\udump下的最新trc文件copy至桌面

C:\Users\LEE\Desktop> tkprof orcl_ora_4516.trc orclb.sql

查看orclb.sql

select * 
from
 test where name=:o

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.04          0          0          0           0
Execute      1      0.00       0.01          0          0          0           0
Fetch        2      0.00       0.00          0          3          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.00       0.05          0          3          0           1

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 58

Rows     Row Source Operation
-------  ---------------------------------------------------
      1  INDEX RANGE SCAN I_TEST (cr=3 pr=0 pw=0 time=51 us)(object id 57877)

到此为止可以可以得出如下结论:
1、无论是否绑定变量,对数据分布不均的情况下柱状图都是很有效的。假如数据是均衡的,没有必要使用直方图。
2、对数据分布不均匀的情况下,使用绑定变量可能会造成恶果,就算对表做了柱状图也一样
3、使用绑定变量,sql第一次执行决定了以后同样的sql执行的执行计划
4、AUTOTRACE的信息不一定准确,必要时要用10046查看需要的信息

本文转自:http://blog.csdn.net/narutobing/article/details/7881082

oracle中直方图的使用的更多相关文章

  1. ORACLE中dba,user,v$等开头的常用表和视图

    一.Oracle表明细及说明1.dba_开头表    dba_users           数据库用户信息    dba_segments    表段信息    dba_extents        ...

  2. Oracle中varchar,varchar2,nvarchar,nvarchar2的区别及其它数据类型描述

    --varchar,varchar2 联系: 1.varchar/varchar2用于存储可变长度的字符串 比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个 ...

  3. Oracle中如何实现Mysql的两表关联update操作

    在看<MySQL 5.1参考手册>的时候,发现MySQL提供了一种两表关联update操作.原文如下: UPDATE items,month SET items.price=month.p ...

  4. ORACLE中的LTRIM、RTRIM和TRIM

    LTRIM.RTRIM和TRIM在ORACLE中的用法:1.LTRIM(C1,C2)其中C1和C2都可以字符串,例如C1是'Miss Liu',C2'MisL'等等.这是第一个和SQL SERVER不 ...

  5. oracle中临时表是用来做什么的

    oracle中临时表是用来做什么的 某些情况下, 需要 多个非常大的表关联的情况下, 但是需要检索的, 是少量的数据的时候.可以先把 大表的数据, 检索出那一小部分, 然后插入到 临时表中, 最后再关 ...

  6. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  7. ORACLE 中ROWNUM用法总结(转)

    ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...

  8. 在Oracle中恢复被DROP掉的表

    在Oracle中可能不小心会DROP掉一个表,如果没有定期做备份的话,将会带来很大的麻烦.如果有的情况下,每天的数据都很重要,而定期备份的周期又稍长,情况恐怕也不容乐观!以前只知道Windows有个回 ...

  9. Oracle中使用REGEXP_SUBSTR,regexp_replace函数

    REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)__srcstr ...

随机推荐

  1. HDU 1024 Max Sum Plus Plus(基础dp)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. ESLint 的使用和.eslintrc.js配置

    在团队协作中,为避免低级 Bug.产出风格统一的代码,会预先制定编码规范.使用 Lint 工具和代码风格检测工具,则可以辅助编码规范执行,有效控制代码质量. ESLint 简介 ESLint 由 Ja ...

  3. POJ 3041 Asteroids (二分图匹配)

    [题目链接] http://poj.org/problem?id=3041 [题目大意] 一个棋盘上放着一些棋子 每次操作可以拿走一行上所有的棋子或者一列上所有的棋子 问几次操作可以拿完所有的棋子 [ ...

  4. HNOI2016 游记

    题外 忽然想起去年的HNOI2015总结里好像引了一句诗: 此情可待成追忆,只是当时已惘然. Day0 唔,感觉不知道想些什么,只是觉得其实还没有做好准备,想学的东西学的仓促,想复习的东西,也只能看一 ...

  5. 【SQL Server 学习系列】-- sql 随机生成中文名字

    原文:[SQL Server 学习系列]-- sql 随机生成中文名字 ,) )) -- 姓氏 ,) )) -- 名字 INSERT @fName VALUES ('赵'),('钱'),('孙'),( ...

  6. 小程序 wx:for 循环嵌套

    json数据: [//library-6F    [//library-6F-601      [//id:1-1 ,8(Y/N),9(Y/N)……21(Y/N)        'Y','Y','Y' ...

  7. Thread.Join(int millisecondsTimeout)

    Join 就是加入的意思,也就是说新创建的线程加入到进程中,并马上执行. 看下面这段代码 Console.WriteLine("start"); Thread myTask = n ...

  8. mac如何挂载移动硬盘、存储设备、U盘

    默认情况下Mac OSX对NTFS磁盘的挂载方式是只读(read-only)的,如何实现读写: 1.借助第三方软件:比如免费版的Mounty 2.因为OSX原生就是支持NTFS的,但是后来由于微软的限 ...

  9. 走进C++程序世界-----operator new delete 重载

     在C++ 的世界里,new 和delete 是keyword.而在C的世界里相相应的malloc和free是函数,关键C++的new和delete分析,详见前面的章节.这里就不在过多的介绍了.链接. ...

  10. 持久化配置管理 diamond 使用简介

    本次为大家介绍diamond的概况和快速使用. 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠.易用,目前淘宝内部绝大多数系统的配置,由diamond来进行统一管 ...