oracle的统计信息的查看与收集
查看某个表的统计信息
- SQL> alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS';
- Session altered.
- SQL> select t.TABLE_NAME,t.NUM_ROWS,t.BLOCKS,t.LAST_ANALYZED from user_tables t where table_name in ('T1','T2');
- TABLE_NAME NUM_ROWS BLOCKS LAST_ANALYZED
- ------------------------------ ---------- ---------- -------------------
- T1 2000 30 2017-07-16 14:02:23
- T2 2000 30 2017-07-16 14:02:23
查看某个表上索引的统计信息
- SQL> select table_name,index_name,t.blevel,t.num_rows,t.leaf_blocks,t.last_analyzed from user_indexes t where table_name in ('T1','T2');
- TABLE_NAME INDEX_NAME BLEVEL NUM_ROWS LEAF_BLOCKS LAST_ANALYZED
- -------------- ---------------------- ---------- ---------- ----------- -------------------
- T1 IDX_T1_OBJ_ID 1 2000 5 2017-07-16 12:06:33
- T2 IDX_T2_OBJ_ID 1 2000 5 2017-07-16 14:02:23
- T2 IDX_T2_OBJ_TYPE 1 2000 5 2017-07-16 14:02:23
- T2 IDX_T2_OBJ_NAME 1 2000 8 2017-07-16 14:02:23
- T2 IDX_T2_DATA_OBJ_ID 1 1198 3 2017-07-16 14:02:23
- T2 IDX_T2_STATUS 1 2000 5 2017-07-16 14:02:23
- T2 IDX_T2_CREATED 1 2000 6 2017-07-16 14:02:23
- T2 IDX_T2_LAST_DDL_TIME 1 2000 6 2017-07-16 14:02:23
- 8 rows selected.
oracle会在一个固定的时间将数据库里的表和索引的相关统计信息进行收集,默认选择周一到周五晚上10点,持续收集4小时,和周六周日早上6点,持续收集20小时。
oracle可以专门对表的记录变化量进行管理,当某表一天记录变化量没有超过指定的阀值时,oracle就不会对该表进行统计信息收集。
修改统计信息自动收集时间
- SQL> set linesize 200
- SQL> col REPEAT_INTERVAL for a60
- SQL> col DURATION for a30
- SQL> select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2
- 2 where t1.window_name=t2.window_name and t2.window_group_name in ('MAINTENANCE_WINDOW_GROUP','BSLN_MAINTAIN_STATS_SCHED');
- WINDOW_NAME REPEAT_INTERVAL DURATION
- ------------------ ------------------------------------------------------------ ---------------
- MONDAY_WINDOW freq=daily;byday=MON;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- TUESDAY_WINDOW freq=daily;byday=TUE;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- WEDNESDAY_WINDOW freq=daily;byday=WED;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- THURSDAY_WINDOW freq=daily;byday=THU;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- FRIDAY_WINDOW freq=daily;byday=FRI;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- SATURDAY_WINDOW freq=daily;byday=SAT;byhour=6;byminute=0; bysecond=0 +000 20:00:00
- SUNDAY_WINDOW freq=daily;byday=SUN;byhour=6;byminute=0; bysecond=0 +000 20:00:00
- 7 rows selected.
- 关闭自动统计信息收集
- BEGIN
- DBMS_SCHEDULER.DISABLE(
- name => '"SYS"."SATURDAY_WINDOW"',
- force => TRUE);
- END;
- /
- 修改自动统计信息持续时间
- BEGIN
- DBMS_SCHEDULER.SET_ATTRIBUTE(
- name => '"SYS"."SATURDAY_WINDOW"',
- attribute => 'DURATION',
- value => numtodsinterval(240,'minute'));
- END;
- /
- 修改自动统计信息开始时间
- BEGIN
- DBMS_SCHEDULER.SET_ATTRIBUTE(
- name => '"SYS"."SATURDAY_WINDOW"',
- attribute => 'REPEAT_INTERVAL',
- value => 'freq=daily;byday=SAT;byhour=22;byminute=0; bysecond=0 ');
- END;
- /
- 开启自动统计信息收集
- BEGIN
- DBMS_SCHEDULER.ENABLE(
- name => '"SYS"."SATURDAY_WINDOW"');
- END;
- /
- SQL> set linesize 200
- SQL> col REPEAT_INTERVAL for a60
- SQL> col DURATION for a30
- SQL> select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2
- 2 where t1.window_name=t2.window_name and t2.window_group_name in ('MAINTENANCE_WINDOW_GROUP','BSLN_MAINTAIN_STATS_SCHED');
- WINDOW_NAME REPEAT_INTERVAL DURATION
- ----------------- ------------------------------------------------------------ --------------
- MONDAY_WINDOW freq=daily;byday=MON;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- TUESDAY_WINDOW freq=daily;byday=TUE;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- WEDNESDAY_WINDOW freq=daily;byday=WED;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- THURSDAY_WINDOW freq=daily;byday=THU;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- FRIDAY_WINDOW freq=daily;byday=FRI;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- SATURDAY_WINDOW freq=daily;byday=SAT;byhour=22;byminute=0; bysecond=0 +000 04:00:00
- SUNDAY_WINDOW freq=daily;byday=SUN;byhour=6;byminute=0; bysecond=0 +000 20:00:00
- 7 rows selected.
手动收集统计信息
收集表统计信息
- exec dbms_stats.gather_table_stats(ownname => 'USER',tabname => 'TEST',estimate_percent => 10,method_opt=> 'for all indexed columns');
- exec dbms_stats.gather_table_stats(ownname => 'USER',tabname => 'TAB_NAME',CASCADE=>TURE);
收集分区表的某个分区统计信息
- exec dbms_stats.gather_table_stats(ownname => 'USER',tabname => 'RANGE_PART_TAB',partname => 'p_201312',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE);
收集索引统计信息
- exec dbms_stats.gather_index_stats(ownname => 'USER',indname => 'IDX_OBJECT_ID',estimate_percent => '10',degree => '4');
收集表和索引统计信息
- exec dbms_stats.gather_table_stats(ownname => 'USER',tabname => 'TEST',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE);
收集某个用户的统计信息
- exec dbms_stats.gather_schema_stats(ownname=>'CS',estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');
收集整个数据库的统计信息
- exec dbms_stats.gather_database_stats(estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');
关于dbms_stats中参数的解释,具体参考官方文档:
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_stats.htm#ARPLS68582
- ownname: USER_NAME
- tabname: TABLE_NAME
- partname: 分区表的某个分区名
- estimate_percent: 采样百分比,有效范围为[0.000001,100]
- block_sample:使用随机块采样代替随机行采样
- method_opt:
- cascade:是否收集此表索引的统计信息
- degree:并行处理的cpu数量
- granularity: 统计数据的收集,'ALL' - 收集所有(子分区,分区和全局)统计信息
动态采集统计信息
对于新创建的表,当访问此表时,oracle会动态的收集这个表的相关信息,等到晚上10点,再将其收集到数据字典中。
- SQL> set autotrace off
- SQL> set linesize 1000
- SQL> drop table t_sample purge;
- drop table t_sample purge
- *
- ERROR at line 1:
- ORA-00942: table or view does not exist
- SQL> create table t_sample as select * from dba_objects;
- Table created.
- SQL> create index idx_t_sample_objid on t_sample(object_id);
- Index created.
新建的表,查不到统计信息
- SQL> select num_rows, blocks, last_analyzed from user_tables where table_name = 'T_SAMPLE';
- NUM_ROWS BLOCKS LAST_ANAL
- ---------- ---------- ---------
- SQL> set autotrace traceonly
- SQL> set linesize 1000
- SQL> select * from t_sample where object_id=20;
- Execution Plan
- ----------------------------------------------------------
- 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)
- Statistics
- ----------------------------------------------------------
- 24 recursive calls
- 0 db block gets
- 93 consistent gets
- 1 physical reads
- 0 redo size
- 1608 bytes sent via SQL*Net to client
- 523 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
- dynamic sampling used for this statement (level=2) 表示动态采样,但是不记录数据字典,除非手动收集表的统计信息。
- SQL> select num_rows, blocks, last_analyzed from user_tables where table_name = 'T_SAMPLE';
- NUM_ROWS BLOCKS LAST_ANAL
- ---------- ---------- ---------
- SQL>
oracle的统计信息的查看与收集的更多相关文章
- oracle学习----统计信息
1.收集统计信息的方式 for all columns size skewonly BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => ...
- Oracle 搜集统计信息
常用的统计信息收集脚本: 非分区表: BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'SCOTT', ...
- Oracle备份统计信息
Oracle可以通过DBMS_STATS.GET_TABLE_STATS 收集表的统计信息,一般的收集方法如下: DBMS_STATS.GATHER_TABLE_STATS(OWNNAME => ...
- Oracle自动统计信息的收集原理及实验
[日期:2014-11-21]来源:Linux社区 作者:stevendbaguo[字体:大 中 小] 从Oracle Database 10g开始,Oracle在建库后就默认创建了一个名为GATH ...
- Oracle Statistic 统计信息 小结
oraclestatisticstabledatabasesqldictionary 目录(?)[-] 直方图上列的信息说明 直方图类型说明 一. Statistic 说明 Oracle 官 ...
- Oracle 11G统计信息自动收集及调整
查询统计信息的收集所对应的task,以及当前状态 col CLIENT_NAME for a50col TASK_NAME for a20SELECT client_name, task_name, ...
- oracle自动统计信息
在11g中,默认自动收集统计信息的时间为晚上10点(周一到周五,4个小时),早上6点(周六,周日,20个小时), select a.window_name, a.repeat_interval,a ...
- oracle更新统计信息以及解锁统计信息
begin dbms_stats.unlock_table_stats(ownname => 'DM_MPAY', tabname => 'PLAT_INFO');end; begin d ...
- Oracle 搜集统计信息的存储过程
DECLARE CURSOR STALE_TABLE IS SELECT OWNER, SEGMENT_NAME, CASE WHEN SIZE_GB < 0.5 THEN 30 WHEN SI ...
随机推荐
- 学习:inets
inets模块 MODULE SUMMARY The inets services API. inets:start():启动应用.
- debugging openstack with pdb
在要开始debug的地方导入pdb: class KeypairAPI(base.Base): """Sub-set of the Compute Manager API ...
- sgu 326(经典网络流构图)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13349 题目大意:有N个球队在同一个赛区,已知他们胜利的场数,还剩 ...
- python入门(十):XML和JSON解析
一.python解析XML 1.xml.dom.*模块,它是W3C DOM API的实现,若需要处理DOM API则该模块很适合,注意xml.dom包里面有许多模块,须区分它们间的不同: 2.xml. ...
- UE4与Visual Studio版本对应关系
首先,要注意,Visual Studio 2015(VS2015)默认安装时并不包含 C++ 的工具.在安装 VS2015 时,需选择 自定义 安装并勾选 C++ 组件,这对之后的工作时必须的.如果已 ...
- post 与get
GET:从服务器上获取数据,也就是所谓的查,仅仅是获取服务器资源,不进行修改. POST:向服务器提交数据,这就涉及到了数据的更新,也就是更改服务器的数据. 补充: PUT:PUT的英文含义是放置,也 ...
- 【CF628D】Magic Numbers 数位DP
[CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...
- Jwt在javaweb项目中的应用核心步骤解读
1.引入jwt依赖 <!--引入JWT依赖,由于是基于Java,所以需要的是java-jwt--> <dependency> <groupId>io.jsonweb ...
- Javascript-可定义位数的十进制转二进制
// 十进制转二进制 function binary (num, Bits) { var d = parseInt(num / 2) var resArry = [num % 2] for(; d ! ...
- Node REPL
Node 自带了交互式解释器,可以执行以下任务: 读取 - 读取用户输入,解析输入了Javascript 数据结构并存储在内存中. 执行 - 执行输入的数据结构 打印 - 输出结果 循环 - 循环操作 ...