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

一、不绑定变量

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. 每天一个Linux命令(10)cp命令

    cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...

  2. mysql悲观锁中的共享锁和排他锁

    概述: 共享锁又称为读锁,简称S锁,顾名思义,共享锁就是多个事务对于同一数据可以共享一把锁,都能访问到数据,但是只能读不能修改. 排他锁又称为写锁,简称X锁,顾名思义,排他锁就是不能与其他所并存,如一 ...

  3. linux-单引号、双引号、反引号的区别。

    一.单引号和双引号 单引号和双引号,都是为了解决中间有空格的问题. 因为空格在Linux中时作为一个很典型的分隔符,比如string1=this is astring,这样执行就会报错.为了避免这个问 ...

  4. Systems Performance: Enterprise and the Cloud 读书笔记系列

    http://blog.csdn.net/xiaonanAndroid/article/category/2557735

  5. 解读TDD的五大误区

    所谓TDD简单地说就是以下两个步骤:确保所有的需求都能被照顾到:在代码不断增加和重构的过程中,可以检查所有的功能是否正确.本文我们一起来看下关于TDD的五大误区. TDD(全称Test Driven ...

  6. tshop-pbsm-shop-nav-ch结构和样式分析

    html结构 <div id="content" class="eshop head-expand tb-shop"> <div id=&qu ...

  7. XShell命令行使用

    1.建立连接: 2.查看总体目录: 3.查看对应服务目录: 4.删除对应jar包后,再查看目录下文件: 5.上传对应的jar文件: 6.重启服务 7.查看服务日志: 8.mv old-name new ...

  8. ECSHOP删除云服务

    一.删除[云服务中心]删除/admin/cloud.php删除/admin/templates/menu.htm中以下代码 Ajax.call('cloud.php?is_ajax=1>act= ...

  9. Spring管理的bean初始化方法的三种方式,以及@PostConstruct不起作用的原因

    1:Spring 容器中的 Bean 是有生命周期的,spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作.下面是常用的三种指定特定操作的方法: 通过实现InitializingBea ...

  10. 5.全局异常捕捉【从零开始学Spring Boot】

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...