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

一、不绑定变量

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. NOIP2018提高组模拟题(二)

    咒语(curse) Description 亮亮梦到自己来到了魔法城堡,但一扇巨大的石门阻拦了他通向城堡内的路. 正当他沮丧之际,突然发现门上有一处机关,机关上有一张很长的纸条. 亮亮拿起纸条的一端, ...

  2. EGS5在linux系统下安装过程

    转载自52MC论坛 作者:xinruibj 平台:Fedora 13 内核版本为:2.6.33, g77版本为:3.4.6: 用户名为xinrui,下面出现这个文件夹xinrui时,修改为你自己的用户 ...

  3. POJ3261 Milk Patterns(二分+后缀数组)

    题目求最长的重复k次可重叠子串. 与POJ1743同理. 二分枚举ans判定是否成立 height分组,如果大于等于ans的组里的个数大于等于k-1,这个ans就可行 #include<cstd ...

  4. Android Developer -- Bluetooth篇 开发实例之三 管理连接

    Managing a Connection When you have successfully connected two (or more) devices, each one will have ...

  5. Java判断中文字符

    package com.jsoft.test; import java.util.regex.Pattern; /** * 判断中文字符 * * @author jim * @date 2017-12 ...

  6. Windows 定时删除指定路径下N天前的日志文件

    Windows 定时删除指定路径下N天前的日志文件 Windows 下bat脚本文件的内容为 1. 删除指定路径下5天前的所有文件 @echo off set SrcDir=E:\WORK\Git s ...

  7. mOOC 编绎原理

    http://mooc.study.163.com/course/YOOKCS0009-1000002001#/info http://study.163.com/series/1001245004. ...

  8. linux之ifconfig命令

    1.命令格式: ifconfig [网络设备] [参数] 2.命令功能: ifconfig 命令用来查看和配置网络设备.当网络环境发生改变时可通过此命令对网络进行相应的配置. 3.命令参数: up 启 ...

  9. 50个最常用的UNIX/Linux命令

    转自http://get.jobdeer.com/493.get 1. tar command examples Create a new tar archive. $ tar cvf archive ...

  10. Android API level 与version对应关系

    https://www.cnblogs.com/jinglecode/p/7753107.html Platform Version API Level VERSION_CODE 中文名称 Andro ...