验证Oracle收集统计信息参数granularity数据分析的力度
生活随笔
收集整理的這篇文章主要介紹了
验证Oracle收集统计信息参数granularity数据分析的力度
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
? ?最近在學(xué)習(xí)Oracle的統(tǒng)計(jì)信息這一塊,收集統(tǒng)計(jì)信息的方法如下:? ?
DBMS_STATS.GATHER_TABLE_STATS ( ownname VARCHAR2, ---所有者名字 tabname VARCHAR2, ---表名 partname VARCHAR2 DEFAULT NULL, ---要分析的分區(qū)名 estimate_percent NUMBER DEFAULT NULL, ---采樣的比例 block_sample BOOLEAN DEFAULT FALSE, ---是否塊分析 method_opt VARCHAR2 DEFAULT ‘FOR ALL COLUMNS SIZE 1’,---分析的方式 degree NUMBER DEFAULT NULL, ---分析的并行度 granularity VARCHAR2 DEFAULT ‘DEFAULT’, ---分析的粒度 cascade BOOLEAN DEFAULT FALSE, ---是否分析索引 stattab VARCHAR2 DEFAULT NULL, ---使用的性能表名 statid VARCHAR2 DEFAULT NULL, ---性能表標(biāo)識(shí) statown VARCHAR2 DEFAULT NULL, ---性能表所有者 no_invalidate BOOLEAN DEFAULT FALSE, ---是否驗(yàn)證游標(biāo)依存關(guān)系 force BOOLEAN DEFAULT FALSE); ---強(qiáng)制分析,即使鎖表? 本文主要對(duì)參數(shù)granularity進(jìn)行了一下驗(yàn)證,
? granularity:數(shù)據(jù)分析的力度
? --global ?---全局
? --partition ---只在分區(qū)級(jí)別做分析
? --subpartition ?--只在子分區(qū)級(jí)別做分析
驗(yàn)證步驟如下:
一、創(chuàng)建一個(gè)分區(qū)表并插入兩條數(shù)據(jù),同時(shí)在字段ID上創(chuàng)建索引 drop table test purge; create table test(id number) partition by range(id) (partition p1 values less than (5), partition p2 values less than (10) ) ; insert into test values(1); insert into test values(6); commit; create index ind_id on test(id); 二、收集表的統(tǒng)計(jì)信息 exec dbms_stats.gather_table_stats(user,'TEST',cascade=>true); 三、查詢表的統(tǒng)計(jì)信息 select num_rows,blocks,last_analyzed from user_tables where table_name = 'TEST'; 結(jié)果如下: num_rows:表數(shù)據(jù)行數(shù) blocks:數(shù)據(jù)塊數(shù) last_analyzed:最近分析時(shí)間 四、查詢表分區(qū)信息 select partition_name,num_rows,blocks,last_analyzed from dba_tab_partitions where table_name ='TEST'; PARTITION_NAME:分區(qū)名稱 NUM_ROWS:數(shù)據(jù)行數(shù) BLOCKS:數(shù)據(jù)塊數(shù) last_analyzed:最近分析時(shí)間 五、查詢索引統(tǒng)計(jì)信息 select num_rows,blevel,last_analyzed from user_indexes where index_name = 'IND_ID'; num_rows:索引數(shù)據(jù)行數(shù) blevel:索引高度 last_analyzed:分析時(shí)間 六、新增一個(gè)分區(qū) alter table test add partition pmax values less than(maxvalue); 七、往新的分區(qū)中插入10000條數(shù)據(jù) begin for i in 1..10000 loop ---插入10000條數(shù)據(jù) insert into test values(100); end loop; commit; end; 八、創(chuàng)建一個(gè)傾斜度非常大的分區(qū) update test set id=10000 where id=100 and rownum=1; ---創(chuàng)造一個(gè)非常傾斜的Pmax分區(qū) Commit; 九、查詢分區(qū)數(shù)據(jù) select id,count(*) from test partition(pmax) group by id; 十、不做分析,再次查詢表的統(tǒng)計(jì)信息 select num_rows,blocks,last_analyzed from user_tables where table_name = 'TEST'; 發(fā)現(xiàn)數(shù)據(jù)行數(shù)量和數(shù)據(jù)塊數(shù)量沒有發(fā)現(xiàn)變化 十一、查詢id=100時(shí)執(zhí)行計(jì)劃 set autotrace traceonly set linesize 1000 select * from test where id=100; 發(fā)現(xiàn)走了索引,正常情況下,因?yàn)閕d=100的數(shù)據(jù)在一個(gè)傾斜度非常高的分區(qū)pmax中,id為100的數(shù)據(jù)有9999條,走索引的代價(jià)會(huì)比走全表的代價(jià)還要高(因?yàn)樽咚饕枰乇?#xff09;,如果統(tǒng)計(jì)信息正確,優(yōu)化器應(yīng)該會(huì)選擇走全表,但是這里沒走全表而是走了索引,這里懷疑是統(tǒng)計(jì)信息不正確導(dǎo)致,后面驗(yàn)證 十二、收集分區(qū)統(tǒng)計(jì)信息 exec dbms_stats.gather_table_stats(user,'TEST',partname => 'PMAX',granularity => 'PARTITION'); 十三、再次查詢表的統(tǒng)計(jì)信息和分區(qū)統(tǒng)計(jì)信息 select partition_name,num_rows,blocks,last_analyzed from dba_tab_partitions where table_name ='TEST'; 發(fā)現(xiàn)和步驟四比較,分區(qū)信息有了變化,說明對(duì)分區(qū)進(jìn)行統(tǒng)計(jì)信息收集后,分區(qū)信息進(jìn)行了更新 select num_rows,blocks,last_analyzed from user_tables where table_name = 'TEST'; 發(fā)現(xiàn)和步驟三想比,表的統(tǒng)計(jì)信息并沒有發(fā)生變化,說明統(tǒng)計(jì)了分區(qū)信息后,表的統(tǒng)計(jì)信息么有更新?
十四、再次查詢id=100的數(shù)據(jù) 仍然走索引,說明在評(píng)估查詢的時(shí)候,表的統(tǒng)計(jì)信息依然陳舊 十五、查詢索引的統(tǒng)計(jì)信息 select num_rows,blevel,last_analyzed from user_indexes where index_name = 'IND_ID'; 發(fā)現(xiàn)索引統(tǒng)計(jì)信息較步驟五沒有變化,說明收集了分區(qū)的統(tǒng)計(jì)信息后,表的索引信息沒有更新?
?
十六、重新再次收集表的統(tǒng)計(jì)信息 exec dbms_stats.gather_table_stats(user,'TEST',cascade =>true); 十七、查詢表的統(tǒng)計(jì)信息以及索引的統(tǒng)計(jì)信息 select num_rows,blocks,last_analyzed from user_tables where table_name = 'TEST'; 表的統(tǒng)計(jì)信息已經(jīng)更新 select num_rows,blevel,last_analyzed from user_indexes where index_name = 'IND_ID'; 索引的統(tǒng)計(jì)信息也已經(jīng)更新 十八、再次查詢id=100的執(zhí)行計(jì)劃 這次發(fā)現(xiàn)走了全表,說明收集了全局的統(tǒng)計(jì)信息后,表的統(tǒng)計(jì)信息準(zhǔn)確了,評(píng)估也就準(zhǔn)確了。轉(zhuǎn)載于:https://www.cnblogs.com/jerome-lamb/p/7535014.html
總結(jié)
以上是生活随笔為你收集整理的验证Oracle收集统计信息参数granularity数据分析的力度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 网页爬取数据生成文字云图
- 下一篇: python 课堂笔记-for语句