oracle中直方图的使用
本文从不绑定变量和绑定变量两种情况讨论直方图的作用
一、不绑定变量
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看
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中直方图的使用的更多相关文章
- ORACLE中dba,user,v$等开头的常用表和视图
一.Oracle表明细及说明1.dba_开头表 dba_users 数据库用户信息 dba_segments 表段信息 dba_extents ...
- Oracle中varchar,varchar2,nvarchar,nvarchar2的区别及其它数据类型描述
--varchar,varchar2 联系: 1.varchar/varchar2用于存储可变长度的字符串 比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个 ...
- Oracle中如何实现Mysql的两表关联update操作
在看<MySQL 5.1参考手册>的时候,发现MySQL提供了一种两表关联update操作.原文如下: UPDATE items,month SET items.price=month.p ...
- ORACLE中的LTRIM、RTRIM和TRIM
LTRIM.RTRIM和TRIM在ORACLE中的用法:1.LTRIM(C1,C2)其中C1和C2都可以字符串,例如C1是'Miss Liu',C2'MisL'等等.这是第一个和SQL SERVER不 ...
- oracle中临时表是用来做什么的
oracle中临时表是用来做什么的 某些情况下, 需要 多个非常大的表关联的情况下, 但是需要检索的, 是少量的数据的时候.可以先把 大表的数据, 检索出那一小部分, 然后插入到 临时表中, 最后再关 ...
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- ORACLE 中ROWNUM用法总结(转)
ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...
- 在Oracle中恢复被DROP掉的表
在Oracle中可能不小心会DROP掉一个表,如果没有定期做备份的话,将会带来很大的麻烦.如果有的情况下,每天的数据都很重要,而定期备份的周期又稍长,情况恐怕也不容乐观!以前只知道Windows有个回 ...
- Oracle中使用REGEXP_SUBSTR,regexp_replace函数
REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)__srcstr ...
随机推荐
- Python的程序结构[4] -> 函数/Function[2] -> 匿名函数
匿名函数 / Anonymous Function 匿名函数是一种不需要绑定函数名的函数 (i.e. functions that are not bound to a name).匿名函数通过 la ...
- POJ 3713 Transferring Sylla (三连通图)
[题目链接] http://poj.org/problem?id=3713 [题目大意] 给出一个图判断是不是三连通图,三连通图的意思是对于图中任意两点, 至少有三条路是可以相互连通的. [题解] 我 ...
- 最近公共祖先LCA Tarjan 离线算法
[简介] 解决LCA问题的Tarjan算法利用并查集在一次DFS(深度优先遍历)中完成所有询问.换句话说,要所有询问都读入后才开始计算,所以是一种离线的算法. [原理] 先来看这样一个性质:当两个节点 ...
- MySQL 中的 base64 函数
1. 5.6版本及之后的版本的base64 主要就是两个mysql内部函数to_base64和from_base64,使用也很简单,如下: 5.6之前不支持 mysql> select vers ...
- Topshelf+Quartz.net+Dapper+Npoi(二)
quartznet 上篇说到quartznet这个东东,topshelf+quartznet有很多不错的文章,可以查看七七同学的文章(http://www.cnblogs.com/jys509/p/4 ...
- UVa116 (单向TSP,多决策问题)
/*----UVa1347 单向TSP 用d(i,j)表示从格子(i,j)出发到最后一列的最小开销 则在(i,j)处有三种决策,d(i,j)转移到d(i-1,j+1),d(i,j+1),d(i+1,j ...
- Eclipse下的java工程目录
对新手来讲,一个Java工程内部的多个文件夹经常会让大家困惑.更可恶的是莫名其妙的路径问题,在Eclipse编写Java程序中,出现频率最高的错误很可能就是路径问题. 这些问题原因其实都是一个,就是关 ...
- JavaWeb项目实现文件下载
File file = new File(path);// path是根据日志路径和文件名拼接出来的 String filename = file.getName();// 获取日志文件名称 Inpu ...
- Hibernate3和4版本的不同
hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了 ...
- Java学习之自动装箱和自动拆箱源码分析
自动装箱(boxing)和自动拆箱(unboxing) 首先了解下Java的四类八种基本数据类型 基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|false ...