oracle 統計資訊檢視與收集
一、檢視錶統計資訊
SQL> alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'; SQL> select t.TABLE_NAME,t.NUM_ROWS,t.BLOCKS,t.LAST_ANALYZED from user_tables t where table_name='WOO';
備註:透過指令碼檢視統計資訊,參考MOS:SCRIPT - Select to show Optimizer Statistics for CBO (Doc ID 31412.1)
二、檢視錶上的索引資訊
select table_name,index_name,t.blevel,t.num_rows,t.leaf_blocks,t.last_analyzed from user_indexes t where table_name='DUMP_TABLE'; SQL> col table_name format a11 SQL> col index_name format a16 SQL> select table_name,index_name,t.blevel,t.num_rows,t.leaf_blocks,t.last_analyzed 2 from user_indexes t 3 where table_name='DUMP_TABLE'; TABLE_NAME INDEX_NAME BLEVEL NUM_ROWS LEAF_BLOCKS LAST_ANAL ----------- ---------------- ---------- ---------- ----------- --------- DUMP_TABLE WORNUM_IND 0 10 1 18-MAR-20
三、檢查當前統計資訊收集策略
set linesize 140 col WINDOW_NAME format a17 col REPEAT_INTERVAL for a55 col DURATION for a15 select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2 where t1.window_name=t2.window_name and t2.window_group_name in ('MAINTENANCE_WINDOW_GROUP','BSLN_MAINTAIN_STATS_SCHED'); SQL> set linesize 140 SQL> col WINDOW_NAME format a17 SQL> col REPEAT_INTERVAL for a55 SQL> col DURATION for a15 SQL> select t1.window_name,t1.repeat_interval,t1.duration from 2 dba_scheduler_windows t1,dba_scheduler_wingroup_members t2 3 where t1.window_name=t2.window_name 4 and t2.window_group_name 5 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.
3.1. 關閉自動統計資訊收集
BEGIN DBMS_SCHEDULER.DISABLE( name => '"SYS"."SATURDAY_WINDOW"', force => TRUE); END; /
3.2 修改自動統計資訊持續時間
BEGIN DBMS_SCHEDULER.SET_ATTRIBUTE( name => '"SYS"."SATURDAY_WINDOW"', attribute => 'DURATION', value => numtodsinterval(180,'minute')); END; /
3.3 修改自動統計資訊開始時間,每週六22點開始
BEGIN DBMS_SCHEDULER.SET_ATTRIBUTE( name => '"SYS"."SATURDAY_WINDOW"', attribute => 'REPEAT_INTERVAL', value => 'freq=daily;byday=SAT;byhour=22;byminute=0;bysecond=0 '); END; /
3.4 開啟自動統計資訊收集
BEGIN DBMS_SCHEDULER.ENABLE( name => '"SYS"."SATURDAY_WINDOW"'); END; /
3.5 再次檢查策略是否正確
set linesize 200 col REPEAT_INTERVAL for a60 col DURATION for a30 select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2 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 20:00:00 SUNDAY_WINDOW freq=daily;byday=SUN;byhour=6;byminute=0; bysecond=0 +000 20:00:00 7 rows selected.
四、手工收集統計資訊
4.1 收集索引統計資訊
exec dbms_stats.gather_index_stats(ownname => 'WOO',indname => 'WORNUM_IND',estimate_percent => '10',degree => '4');
4.2 收集表和索引統計資訊
exec dbms_stats.gather_table_stats(ownname => 'WOO',tabname => 'DUMP_TABLE',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE);
4.3 收集表的統計資訊
exec dbms_stats.gather_table_stats(ownname => 'WOO',tabname => 'DUMP_TABLE',estimate_percent => 10,method_opt=> 'for all indexed columns');
4.4 收集分割槽表統計資訊
exec dbms_stats.gather_table_stats(ownname => 'WOO',tabname => 'DUMP_TABLE',partname => 'p_20190318',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE);
4.5 收集某個使用者的統計資訊
exec dbms_stats.gather_schema_stats(ownname=>'WOO',estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');
4.6 收集整個資料庫的統計資訊
exec dbms_stats.gather_database_stats(estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');
五、動態取樣:
對於新建立的表,當訪問此表時,oracle會動態的收集這個表的相關資訊,等到晚上10點,再將其收集到資料字典中。
SQL> set linesize 200 SQL> set autotrace traceonly SQL> select * from DUMP_TABLE; 10 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1795212136 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10 | 20390 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| DUMP_TABLE | 10 | 20390 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------------- Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 4 recursive calls 0 db block gets 9 consistent gets 0 physical reads 0 redo size 1305 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) 10 rows processed
- dynamic sampling used for this statement (level=2) 表示動態取樣,但是不記錄資料字典,除非手動收集表的統計資訊。
我們們透過user_tables檢視結果也是一樣的
SQL> set autotrace off; SQL> select num_rows, blocks, last_analyzed from user_tables where table_name = 'DUMP_TABLE'; NUM_ROWS BLOCKS LAST_ANAL ---------- ---------- --------- SQL>
六、統計資訊收集完之後:
SQL> set linesize 200 SQL> set autotrace traceonly SQL> select * from DUMP_TABLE; 10 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1795212136 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10 | 470 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| DUMP_TABLE | 10 | 470 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------------- Statistics ---------------------------------------------------------- 33 recursive calls 0 db block gets 56 consistent gets 0 physical reads 0 redo size 1305 bytes sent via SQL*Net to client 523 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 6 sorts (memory) 0 sorts (disk) 10 rows processed SQL> set autotrace off; SQL> alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'; SQL> select num_rows, blocks, last_analyzed from user_tables where table_name = 'DUMP_TABLE'; NUM_ROWS BLOCKS LAST_ANALYZED ---------- ---------- ------------------- 10 4 2020-03-18 17:06:16 SQL>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20674423/viewspace-2681049/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle檢視查詢慢之統計資訊收集Oracle
- ORACLE 統計資訊的收集與管理Oracle
- Oracle收集統計資訊Oracle
- Oracle 統計資訊收集Oracle
- 收集oracle統計資訊Oracle
- ORACLE 收集統計資訊Oracle
- 【統計資訊】Oracle常用的收集統計資訊方式Oracle
- Oracle統計資訊自動收集Oracle
- oracle收集統計資訊job停止Oracle
- Oracle錶的歷史統計資訊檢視Oracle
- oracle檢視和更新統計表的資訊Oracle
- oracle 11g統計資訊收集Oracle
- [Oracle] Oracle收集統計資訊的取樣比例Oracle
- oracle 11g 統計資訊 相關檢視Oracle
- Oracle運維指令碼-收集統計資訊Oracle運維指令碼
- oracle統計資訊--直方圖的收集:Oracle直方圖
- 重新收集oracle表的統計資訊Oracle
- 關於oracle自動收集統計資訊Oracle
- Oracle統計資訊的收集和維護Oracle
- 【統計資訊】全面檢視錶所有統計資訊SQLSQL
- Oracle11g 統計資訊-----統計資訊自動收集任務Oracle
- Oracle11g 統計資訊——統計資訊自動收集任務Oracle
- 【Oracle】11g Oracle自動收集統計資訊Oracle
- Oracle 11g手工收集表統計資訊Oracle
- Oracle 11g 統計資訊收集指令碼Oracle指令碼
- ORACLE 11g 自動收集統計資訊Oracle
- ORACLE表統計資訊與列統計資訊Oracle
- ORACLE表統計資訊與列統計資訊、索引統計資訊Oracle索引
- Oracle 11g 建立臨時window時間視窗 收集統計資訊Oracle
- [sqlserver] 檢視錶的統計資訊SQLServer
- [統計資訊系列7] Oracle 11g的自動統計資訊收集Oracle
- 企業微信教程:如何匯出和檢視收集到的資訊統計
- oracle統計資訊 使用dbms_stats包收集統計資料Oracle
- Oracle ASM檢視資訊OracleASM
- Oracle收集統計資訊之NO_INVALIDATE引數Oracle
- Oracle 11g 之自動收集統計資訊Oracle
- Oracle10g 統計資訊的自動收集Oracle
- ORACLE10g自動收集CBO統計資訊Oracle