這篇文章主要講解了“oracle統(tǒng)計信息查看與收集的方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“oracle統(tǒng)計信息查看與收集的方法是什么”吧!
創(chuàng)新互聯(lián)建站致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括網(wǎng)站設(shè)計、網(wǎng)站制作、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)建站為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)建站核心團隊十余年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗,為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。
一、查看表統(tǒng)計信息
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';
備注:通過腳本查看統(tǒng)計信息,參考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
三、檢查當(dāng)前統(tǒng)計信息收集策略
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. 關(guān)閉自動統(tǒng)計信息收集
BEGIN DBMS_SCHEDULER.DISABLE( name => '"SYS"."SATURDAY_WINDOW"', force => TRUE); END; /
3.2 修改自動統(tǒng)計信息持續(xù)時間
BEGIN DBMS_SCHEDULER.SET_ATTRIBUTE( name => '"SYS"."SATURDAY_WINDOW"', attribute => 'DURATION', value => numtodsinterval(180,'minute')); END; /
3.3 修改自動統(tǒng)計信息開始時間,每周六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 開啟自動統(tǒng)計信息收集
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.
四、手工收集統(tǒng)計信息
4.1 收集索引統(tǒng)計信息
exec dbms_stats.gather_index_stats(ownname => 'WOO',indname => 'WORNUM_IND',estimate_percent => '10',degree => '4');
4.2 收集表和索引統(tǒng)計信息
exec dbms_stats.gather_table_stats(ownname => 'WOO',tabname => 'DUMP_TABLE',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE);
4.3 收集表的統(tǒng)計信息
exec dbms_stats.gather_table_stats(ownname => 'WOO',tabname => 'DUMP_TABLE',estimate_percent => 10,method_opt=> 'for all indexed columns');
4.4 收集分區(qū)表統(tǒng)計信息
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 收集某個用戶的統(tǒng)計信息
exec dbms_stats.gather_schema_stats(ownname=>'WOO',estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');
4.6 收集整個數(shù)據(jù)庫的統(tǒng)計信息
exec dbms_stats.gather_database_stats(estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');
五、動態(tài)采樣:
對于新創(chuàng)建的表,當(dāng)訪問此表時,oracle會動態(tài)的收集這個表的相關(guān)信息,等到晚上10點,再將其收集到數(shù)據(jù)字典中。
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) 表示動態(tài)采樣,但是不記錄數(shù)據(jù)字典,除非手動收集表的統(tǒng)計信息。
咱們通過user_tables查看結(jié)果也是一樣的
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>
六、統(tǒng)計信息收集完之后:
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>
感謝各位的閱讀,以上就是“oracle統(tǒng)計信息查看與收集的方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對oracle統(tǒng)計信息查看與收集的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!