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 ...
随机推荐
- 洛谷——P1170 兔八哥与猎人
P1170 兔八哥与猎人 题目描述 兔八哥躲藏在树林旁边的果园里.果园有M × N棵树,组成一个M行N列的矩阵,水平或垂直相邻的两棵树的距离为1.兔八哥在一棵果树下. 猎人背着猎枪走进了果园,他爬上一 ...
- 网络协议图形化分析工具EtherApe
网络协议图形化分析工具EtherApe 在对网络数据分析的时候,渗透测试人员往往只关心数据流向以及协议类型,而不关心具体数据包的内容.因为这样可以快速找到网络的关键节点或者重要的协议类型. Kal ...
- iOS 自定义字体设置
有时候客户都要求使用方正兰亭刊黑字体,可是iOS没有自带这个字体,肿么办 ,只能自己自定义字体了,下面是自定义字体的几个重要步骤: 1.下载字体资源文件(.ttf或.otf格式的文件) 比如说你要使用 ...
- 解决android模拟器连接本机服务器”Connection refused”问题
在本机用模拟器连接 localhost 的服务器不成功,经查询是我反了一个小错误. android 模拟器其本身的localhost就是它自己的ip,而如果我要连接本机的localhost则需要将 ...
- Swagger简介,轻松构造restful api的文档
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使 ...
- squid.con 配置文件详解
博客转载:http://www.articleswriting.net/article/6477447043/;jsessionid=42C9702B475ECF99EB861214186390E8 ...
- scrapy爬虫 函数间传值简易教程
2017-03-27 有的时候我们爬取数据的时候需要在多个页面之间跳转,爬取完所有页面的数据的时候才能把所有数据一起存到数据库,这个时候我们就需要把某个函数内爬取的数据传到下一个函数当中.有人可能会说 ...
- POJ 2503-Babelfish(map)
题目地址:POJ 2503 题意:输入一个字典.字典格式为"英语 外语"的一一映射关系然后输入若干个外语单词.输出他们的 英语翻译单词,假设字典中不存在这个单词,则输出" ...
- iOS---sha1加密的一个坑
OC语言写的sha1加密算法,在网上随手能够搜索到(例如以下便是),可是我不得不说有一些人不责任,没有提醒大家导入必要的系统头文件.从而导致错误 + (NSString *) sha1:(NSStri ...
- demo 微信毛玻璃效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...