当前位置: 首页 > news >正文

执行计划基础 动态采样

1. 统计信息默认情况下是每天晚上10点半后收集,如果新建对象还没来得级收集统计信息,就采用动态采样的方式。
2. 具体在set autotrace 跟踪的执行计划中,可以看到类似:- dynamic sampling used for this statement (level=2)
3. 除非你用类似/*+dynamic_sampling(t 0) */的HINT关闭这个动态采样。
4. 在收集过统计信息后,Oracle就不会采用动态采样。
注:建索引过程中,默认会收集索引相关的统计信息。

 

set autotrace off
set linesize 1000
drop table t_sample purge;
create table t_sample as select * from dba_objects;
create index idx_t_sample_objid on t_sample(object_id);
select num_rows, blocks, last_analyzed from user_tables where table_name = 'T_SAMPLE'; NUM_ROWS BLOCKS LAST_ANALYZED ----------------------------------

没有统计信息
--建索引后,自动收集统计信息。 select index_name, num_rows, leaf_blocks, distinct_keys, last_analyzed from user_indexes where table_name = 'T_SAMPLE'; INDEX_NAME NUM_ROWS LEAF_BLOCKS DISTINCT_KEYS LAST_ANALYZED ------------------------------ ---------- ----------- ------------- -------------- IDX_T_SAMPLE_OBJID 73159 162 73159 11-1月 -14 set autotrace traceonly set linesize 1000 select * from t_sample where object_id=20; 执行计划 ---------------------------------------------------------- Plan hash value: 1453182238 -------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T_SAMPLE | 1 | 207 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T_SAMPLE_OBJID | 1 | | 1 (0)| 00:00:01 | -------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=20) Note ----- - dynamic sampling used for this statement (level=2) 统计信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 1393 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed --以下方法是要取消动态采样。 select /*+dynamic_sampling(t 0) */ * from t_sample t where object_id=20; 执行计划 ---------------------------------------------------------- Plan hash value: 1453182238 -------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 872 | 176K| 6 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T_SAMPLE | 872 | 176K| 6 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T_SAMPLE_OBJID | 349 | | 1 (0)| 00:00:01 | -------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=20) 统计信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 1393 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed --收集统计信息 exec dbms_stats.gather_table_stats(ownname => 'LJB',tabname => 'T_SAMPLE',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE) ; set autotrace off
--显示表的统计信息 select num_rows, blocks, last_analyzed from user_tables where table_name = 'T_SAMPLE'; NUM_ROWS BLOCKS LAST_ANALYZED ---------- ---------- -------------- 73630 1068 12-1月 -14 set autotrace traceonly select * from t_sample where object_id=20; 执行计划 ------------------------------------------------------------------------------------------------- Plan hash value: 1453182238 -------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 97 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T_SAMPLE | 1 | 97 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T_SAMPLE_OBJID | 1 | | 1 (0)| 00:00:01 | -------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=20) 统计信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 1393 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed --表统计信息收集后,是这样的。 set autotrace off select num_rows, blocks, last_analyzed from user_tables where table_name = 'T'; NUM_ROWS BLOCKS LAST_ANALYZED --------- ---------- -------------- 73118 1068 11-1月 -14

 检查哪些未被收集或者很久没收集(表、分区、子分区)

select table_name, blocks, num_rows, last_analyzed
  from user_tab_statistics t
 where (t.last_analyzed is null or t.last_analyzed < sysdate - 100)
   and table_name not like 'BIN$%'
   order by last_analyzed ;
   
   

select table_name, blocks, num_rows, last_analyzed
  from user_tab_partitions t
 where (t.last_analyzed is null or t.last_analyzed < sysdate - 100)
   and table_name not like 'BIN$%'
   order by last_analyzed ;


select table_name, blocks, num_rows, last_analyzed
  from user_tab_subpartitions t
 where (t.last_analyzed is null or t.last_analyzed < sysdate - 100)
   and table_name not like 'BIN$%'
   order by last_analyzed ;

 检查哪些索引未被收集或者很久没收集

select t.table_name,
       t.index_name,
       t.blevel,
       t.leaf_blocks,
       t.num_rows,
       t.last_analyzed
  from user_ind_statistics t
 where (t.last_analyzed is null or t.last_analyzed < sysdate - 100)
   and table_name not like 'BIN$%'
 order by table_name,index_name;

临时表不要收集统计信息,会导致执行计划出错。

排查被收集统计信息的临时表

select table_name, 
       t.last_analyzed, 
       t.num_rows,
       t.blocks
  from user_tables t
where t.temporary = 'Y'
   and last_analyzed is not null;

 

转载于:https://www.cnblogs.com/yg_zhang/p/3840307.html

相关文章:

  • 浅谈UML的概念和模型之UML九种图
  • iOS开发百问(4)
  • 使用mysqldump导入导出MySQL数据库
  • perl:warning:Setting locale failed解决办法
  • tomcat启用远程调试
  • 源码安装nginx及简单应用
  • C++的异常处理之二:C++11的异常处理
  • wamp环境如何使用ip访问站点
  • 轻量级表达式树解析框架Faller
  • 写在工作满三年的时候
  • 016# Adempiere系统的CRP-产能需求计划
  • myeclipse中文编码错误,没有GBK选项
  • sql install error
  • tomcat-cpu利用率超高的解决
  • Oracle存储过程中——PLS-00103
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • bootstrap创建登录注册页面
  • dva中组件的懒加载
  • Java读取Properties文件的六种方法
  • js写一个简单的选项卡
  • MaxCompute访问TableStore(OTS) 数据
  • Mybatis初体验
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • React中的“虫洞”——Context
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Spring核心 Bean的高级装配
  • SwizzleMethod 黑魔法
  • vue.js框架原理浅析
  • windows下使用nginx调试简介
  • 飞驰在Mesos的涡轮引擎上
  • 利用DataURL技术在网页上显示图片
  • 每天一个设计模式之命令模式
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 学习使用ExpressJS 4.0中的新Router
  • 一文看透浏览器架构
  • 字符串匹配基础上
  • ionic入门之数据绑定显示-1
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (function(){})()的分步解析
  • (Note)C++中的继承方式
  • (二)windows配置JDK环境
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • ***利用Ms05002溢出找“肉鸡
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .NET国产化改造探索(一)、VMware安装银河麒麟
  • .NET开发者必备的11款免费工具