Oracle优化07-分析及动态采样-DBMS_STATS 包
思維導(dǎo)圖
07系列文章
Oracle優(yōu)化07-分析及動(dòng)態(tài)采樣-直方圖
Oracle優(yōu)化07-分析及動(dòng)態(tài)采樣-DBMS_STATS 包
Oracle優(yōu)化07-分析及動(dòng)態(tài)采樣-動(dòng)態(tài)采樣
DBMS_STATS包
DBMS_STAS 包不僅能夠?qū)Ρ磉M(jìn)行分析,它還可以對(duì)數(shù)據(jù)庫(kù)分析進(jìn)行管理。
按照功能可以分一下幾類(lèi):
( 1) 性能數(shù)據(jù)的收集
( 2) 性能數(shù)據(jù)的設(shè)置
( 3) 性能數(shù)據(jù)的刪除
( 4) 性能數(shù)據(jù)的備份和恢
11G 官方文檔:
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_stats.htm#ARPLS059
性能數(shù)據(jù)的收集包含這樣幾個(gè)存儲(chǔ)過(guò)程:
- GATHER_DATABASE_STATS Procedures
- GATHER_DICTIONARY_STATS Procedure
- GATHER_FIXED_OBJECTS_STATS Procedure
- GATHER_INDEX_STATS Procedure
- GATHER_SCHEMA_STATS Procedures
- GATHER_SYSTEM_STATS Procedures
- GATHER_TABLE_STATS Procedure
從名字也可以看出各自的作用,這些存儲(chǔ)過(guò)程用來(lái)收集數(shù)據(jù)庫(kù)不同級(jí)別對(duì)象的性能數(shù)據(jù),包括:數(shù)據(jù)庫(kù),數(shù)據(jù)字典,表,索引,SCHEMA 的性能等。
GATHER_TABLE_STATS 收集信息功能
我們分析時(shí)最常用到的就是GATHER_TABLE_STATS 了。
在11gR2版本中dbms_stats包下的 gather_table_stats存過(guò)定義如下
我們可以在注釋中看到 This procedure gathers table and column (and index) statistics.
procedure gather_table_stats(ownname varchar2, tabname varchar2, partname varchar2 default null,estimate_percent number default DEFAULT_ESTIMATE_PERCENT,block_sample boolean default FALSE,method_opt varchar2 default DEFAULT_METHOD_OPT,degree number default to_degree_type(get_param('DEGREE')),granularity varchar2 default DEFAULT_GRANULARITY,cascade boolean default DEFAULT_CASCADE,stattab varchar2 default null, statid varchar2 default null,statown varchar2 default null,no_invalidate boolean defaultto_no_invalidate_type(get_param('NO_INVALIDATE')),stattype varchar2 default 'DATA',force boolean default FALSE,-- the context is intended for internal use only. context dbms_stats.CContext default null);-- -- This procedure gathers table and column (and index) statistics. -- It attempts to parallelize as much of the work as possible, but there -- are some restrictions as described in the individual parameters. -- This operation will not parallelize if the user does not have select -- privilege on the table being analyzed. -- -- Input arguments: -- ownname - schema of table to analyze -- tabname - name of table -- partname - name of partition -- estimate_percent - Percentage of rows to estimate (NULL means compute). -- The valid range is [0.000001,100]. Use the constant -- DBMS_STATS.AUTO_SAMPLE_SIZE to have Oracle determine the -- appropriate sample size for good statistics. This is the default. -- The default value can be changed using set_param procedure. -- block_sample - whether or not to use random block sampling instead of -- random row sampling. Random block sampling is more efficient, but -- if the data is not randomly distributed on disk then the sample values -- may be somewhat correlated. Only pertinent when doing an estimate -- statistics. -- method_opt - method options of the following format -- -- method_opt := FOR ALL [INDEXED | HIDDEN] COLUMNS [size_clause] -- FOR COLUMNS [size_clause] -- column|attribute [size_clause] -- [,column|attribute [size_clause] ... ] -- -- size_clause := SIZE [integer | auto | skewonly | repeat], -- where integer is between 1 and 254 -- -- column := column name | extension name | extension -- -- default is FOR ALL COLUMNS SIZE AUTO. -- The default value can be changed using set_param procedure. -- Optimizer related table statistics are always gathered. -- -- If an extension is provided, the procedure create the extension if it -- does not exist already. Please refer to create_extended_stats for -- description of extension. -- -- degree - degree of parallelism (NULL means use of table default value -- which was specified by DEGREE clause in CREATE/ALTER TABLE statement) -- Use the constant DBMS_STATS.DEFAULT_DEGREE for the default value -- based on the initialization parameters. -- default for degree is NULL. -- The default value can be changed using set_param procedure. -- granularity - the granularity of statistics to collect (only pertinent -- if the table is partitioned) -- 'AUTO' - the procedure determines what level of statistics to collect -- 'GLOBAL AND PARTITION' - gather global- and partition-level statistics -- 'APPROX_GLOBAL AND PARTITION' - This option is similar to -- 'GLOBAL AND PARTITION'. But the global statistics are aggregated -- from partition level statistics. It will aggregate all statistics except number of -- distinct values for columns and number of distinct keys of indexes. -- The existing histograms of the columns at the table level -- are also aggregated.The global statistics are gathered -- (i.e., going back to GLOBAL AND PARTITION behaviour) -- if partname argument is null. The aggregation will use only -- partitions with statistics, so to get accurate global statistics, -- user has to make sure to have statistics for all partitions. -- -- -- This option is useful when you collect statistics for a new partition added -- into a range partitioned table (for example, a table partitioned by month). -- The new data in the partition makes the global statistics stale (especially -- the min/max values of the partitioning column). This stale global statistics -- may cause suboptimal plans. In this scenario, users can collect statistics -- for the newly added partition with 'APPROX_GLOBAL AND PARTITION' -- option so that the global statistics will reflect the newly added range. -- This option will take less time than 'GLOBAL AND PARTITION' option since the -- global statistics are aggregated from underlying partition level statistics. -- Note that, if you are using APPROX_GLOBAL AND PARTITION, -- you still need to collect global statistics (with granularity = 'GLOBAL' option) -- when there is substantial amount of change at the table level. -- For example you added 10% more data to the table. This is needed to get the -- correct number of distinct values/keys statistic at table level. -- 'SUBPARTITION' - gather subpartition-level statistics -- 'PARTITION' - gather partition-level statistics -- 'GLOBAL' - gather global statistics -- 'ALL' - gather all (subpartition, partition, and global) statistics -- default for granularity is AUTO. -- The default value can be changed using set_param procedure. -- cascade - gather statistics on the indexes for this table. -- Use the constant DBMS_STATS.AUTO_CASCADE to have Oracle determine -- whether index stats to be collected or not. This is the default. -- The default value can be changed using set_param procedure. -- Using this option is equivalent to running the gather_index_stats -- procedure on each of the table's indexes. -- stattab - The user stat table identifier describing where to save -- the current statistics. -- statid - The (optional) identifier to associate with these statistics -- within stattab. -- statown - The schema containing stattab (if different then ownname) -- no_invalidate - Do not invalide the dependent cursors if set to TRUE. -- The procedure invalidates the dependent cursors immediately -- if set to FALSE. -- Use DBMS_STATS.AUTO_INVALIDATE to have oracle decide when to -- invalidate dependend cursors. This is the default. The default -- can be changed using set_param procedure. -- When the 'cascade' argument is specified, not pertinent with certain -- types of indexes described in the gather_index_stats section. -- force - gather statistics of table even if it is locked. -- context - internal use only. -- -- Exceptions: -- ORA-20000: Table does not exist or insufficient privileges -- ORA-20001: Bad input value -- ORA-20002: Bad user statistics table, may need to upgrade it -- ORA-20005: object statistics are locked -- OWNNAME: 要分析表的擁有者。 TABNAME: 要分析的表名。 PARTNAME: 分區(qū)的名字,只對(duì)分區(qū)表或分區(qū)索引有用。 ESTIMATE_PERCENT: 采樣行的百分比,取值范圍[0.000001,100],使用常DBMS_STATS.AUTO_SAMPLE_SIZE讓ORACLE決定適合的采樣大小, 這也是默認(rèn)值,可以使用DBMS_STATS.SET_PARAM進(jìn)行修改默認(rèn)值。NULL可以讓ORACLE采樣全部數(shù)據(jù) BLOCK_SAMPLE: 是否采用隨即塊采樣代替行隨即行采樣。 METHOD_OPT: 決定HISTOGRAMS信息是怎樣被統(tǒng)計(jì)的.METHOD_OPT的取值如下: FOR ALL COLUMNS:統(tǒng)計(jì)所有列的HISTOGRAMS. FOR ALL INDEXED COLUMNS:統(tǒng)計(jì)所有INDEXED列的HISTOGRAMS. FOR ALL HIDDEN COLUMNS:統(tǒng)計(jì)你看不到列的HISTOGRAMS FOR COLUMNS <LIST> SIZE <INTEGER> | REPEAT | AUTO | INTEGER指的直方圖的BUCKETS數(shù)量,取值范圍為[1,254]。 REPEAT上次統(tǒng)計(jì)過(guò)的HISTOGRAMS。 AUTO:ORACLE根據(jù)列數(shù)據(jù)的分布及相關(guān)列的訪問(wèn)量來(lái)決定收集直方圖的列。 SKEWONLY:ORACLE 根據(jù)列的數(shù)據(jù)分布來(lái)決定哪些列收集直方圖 。 DEGREE: 并行度 GRANULARITY: 收集統(tǒng)計(jì)信息的粒度。(只應(yīng)用于分區(qū)表),值包括: 'ALL':搜集(SUBPARTITION,PARTITION,AND GLOBAL)統(tǒng)計(jì)信息。 'AUTO':基于分區(qū)的類(lèi)型來(lái)決定粒度,默認(rèn)值。 'DEFAULT':收集GLOBAL和PARTITION LEVEL的統(tǒng)計(jì)信息,等同與'GLOBAL AND PARTITION'。 'GLOBAL':收集全局統(tǒng)計(jì)信息 'GLOBAL AND PARTITION':收集GLOBAL和PARTITION LEVEL統(tǒng)計(jì)信息。 'GPARTITION':收集PARTITION-LEVEL的統(tǒng)計(jì)信息。 'SUBPARTITION':收集SUBPARTITION-LEVEL統(tǒng)計(jì)信息 。 CASCADE: 收集索引的統(tǒng)計(jì)信息。是收集索引的信息.默認(rèn)為FALSE. STATTAB : 設(shè)置存儲(chǔ)統(tǒng)計(jì)信息的表 STATID : 表的ID STATOWN :表的OWNER NO_INVALIDATE: 若是設(shè)置為T(mén)RUE,則Oracle不會(huì)立馬使 dependent Cursors失效,若設(shè)置為FALSE,Oracle則會(huì)立馬使dependent Cursor失效. FORCE: 即使鎖住也要收集表的統(tǒng)計(jì)信息gather_table_STATS使用
在 gather_table_stats 存儲(chǔ)過(guò)程的所有參數(shù)中,除了 ownname 和 tabname,其他的參數(shù)都有默認(rèn)值。
所以我們?cè)谡{(diào)用這個(gè)存儲(chǔ)過(guò)程時(shí), Oracle 會(huì)使用參數(shù)的默認(rèn)值對(duì)表進(jìn)行分析。
如:
SQL> exec dbms_stats.gather_table_STATS(user,'T');PL/SQL procedure successfully completedoracel就會(huì)對(duì)當(dāng)前用戶下的T表按照所有參數(shù)的默認(rèn)值進(jìn)行分析,其中user是一個(gè)變量,用來(lái)返回當(dāng)前的用戶信息。
當(dāng)然你也可以指定用戶名,比如:
SQL> exec dbms_stats.gather_table_STATS('cc','xgj');PL/SQL procedure successfully completed對(duì)cc用戶下的xgj表進(jìn)行分析。
查看gather_table_STATS參數(shù)當(dāng)前的默認(rèn)值
如果想查看當(dāng)前的默認(rèn)值,可以使用 dbms_stats.get_param 函數(shù)來(lái)獲取:
比如查看method_opt的默認(rèn)值:
SQL> select dbms_stats.get_param('method_opt') from dual;參數(shù)說(shuō)明
參數(shù)的說(shuō)明:
estimate_percent 參數(shù)
這個(gè)參數(shù)是一個(gè)百分比值,它告訴分析包需要使用表中數(shù)據(jù)的多大比例來(lái)做分析。
理論上來(lái)講,采樣的數(shù)據(jù)越多,得到的信息就越接近于實(shí)際, CBO做出的執(zhí)行計(jì)劃就越優(yōu)化,但是,采樣越多,消耗的系統(tǒng)資源必然越多。 對(duì)系統(tǒng)的影響也越大。
所以對(duì)于這個(gè)值的設(shè)置,要根據(jù)業(yè)務(wù)情況來(lái)。
如果數(shù)據(jù)的直方圖分布比較均勻,就可以使用默認(rèn)值: AUTO_SAMPLE_SIZE,即讓 Oracle 自己來(lái)判斷采樣的比例。
有時(shí),特別是對(duì)于批量加載的表,我們可以預(yù)估表中的數(shù)據(jù)量,可以人工地設(shè)置一個(gè)合理的值。 一般,對(duì)于一個(gè)有 1000 萬(wàn)數(shù)據(jù)的表分區(qū),可以把這個(gè)參數(shù)設(shè)置為 0.000001.
Method_option 參數(shù)
這個(gè)參數(shù)用來(lái)定義直方圖分析的一些值。
FOR ALL [INDEXED | HIDDEN] COLUMNS [size_clause]
FOR COLUMNS [size clause] column|attribute [size_clause] [,column|attribute
[size_clause]…]
這里給出了 4 種指定哪些列進(jìn)行分析的方式:
( 1) 所有列: for all column
( 2) 索引列:只對(duì)有索引的列進(jìn)行分析, for all indexed columns
( 3) 影藏列:只對(duì)影藏的列進(jìn)行分析, for all hidden columns
( 4) 顯 示 指 定 列 : 顯 示 的 指 定 那 些 列 進(jìn) 行 分 析 , for columns columns_name
該參數(shù)默認(rèn)值: for all columns size auto.
degree 參數(shù)
用來(lái)指定分析時(shí)使用的并行度。 有以下這些設(shè)置:
(1) Null: 如果設(shè)置為 null, Oracle 將使用被分析表屬性的并行度,比如表在創(chuàng)建時(shí)指定的并行度,或者后者使用 alter table 重新設(shè)置的并行度。
(2) 一個(gè)數(shù)值: 可以顯示地指定分析時(shí)使用的并行度。
(3) Default_degree: 如果設(shè)置為 default, Oracle 將根據(jù)初始化參數(shù)中相關(guān)參數(shù)的設(shè)置來(lái)決定使用的并行度。
這個(gè)參數(shù)的默認(rèn)值是 Null.即通過(guò)表上的并行度屬性來(lái)決定分析使用的并行度。
當(dāng)需要分析的表或表分區(qū)非常大,并且系統(tǒng)資源比較充分的時(shí)候,就可以考慮使用并行的方式來(lái)做分析,這樣就會(huì)大大提高分析的速度。 相反,如果你的系統(tǒng)資源比較吃緊,那么啟用并行可能會(huì)適得其反。
no_invalidate
no_invalidate:缺省值是DBMS_STATS.AUTO_INVALIDATE.
如果想要dbms_stats分析立馬見(jiàn)效,需要使用no_invalidate=false option或者DBA自己手工invalidate游標(biāo)。
Granularity
分析的粒度,有以下幾個(gè)配置:
( 1) ALL : 將會(huì)對(duì)表的全局( global),分區(qū),子分區(qū)的數(shù)據(jù)都做分析
( 2) AUTO: Oracle 根據(jù)分區(qū)的類(lèi)型,自動(dòng)決定做哪一種粒度的分析。
( 3) GLOBAL:只做全局級(jí)別的分析。
( 4) GLOBAL AND PARTITION: 只對(duì)全局和分區(qū)級(jí)別做分析,對(duì)子分區(qū)不做分析,這是和 ALL 的一個(gè)區(qū)別。
( 5) PARTITION: 只在分區(qū)級(jí)別做分析。
( 6) SUBPARTITION: 只在子分區(qū)做分析。
在生產(chǎn)環(huán)境中,特別是 OLAP 或者數(shù)據(jù)倉(cāng)庫(kù)的環(huán)境中,這個(gè)參數(shù)的設(shè)置會(huì)直接影響到 CBO 的執(zhí)行計(jì)劃選擇。
在 OLAP 或者數(shù)據(jù)倉(cāng)庫(kù)系統(tǒng)中,經(jīng)常有這樣的事情,新創(chuàng)建一個(gè)分區(qū),將批量的數(shù)據(jù)(通常是很大的數(shù)據(jù))加載到分區(qū)中,對(duì)分區(qū)做分析,然后做報(bào)表或者數(shù)據(jù)挖掘。
在理想的情況下,對(duì)表的全局,分區(qū)都做分析,這樣才能得到最充
足的數(shù)據(jù),但是通常這樣的表都非常大,如果每增加一個(gè)分區(qū)都需要做一次全局分析,那么會(huì)消耗極大的系統(tǒng)資源。
但是如果只對(duì)新加入的分區(qū)進(jìn)行分區(qū)而不做全局分析, oracle 在全局范圍內(nèi)的信息就會(huì)不準(zhǔn)確。
該參數(shù)在默認(rèn)情況下, DBMS_STATS 包會(huì)對(duì)表級(jí)(全局),分區(qū)級(jí)(對(duì)應(yīng)參數(shù) partition)都會(huì)進(jìn)行分析。
如果把 cascade 設(shè)置為 true,相應(yīng)索引的全局和分區(qū)級(jí)別也都會(huì)被分析。
如果只對(duì)分區(qū)級(jí)進(jìn)行分析,而全局沒(méi)有分析,那么全局信息沒(méi)有更新,依然會(huì)導(dǎo)致 CBO 作出錯(cuò)誤的執(zhí)行計(jì)劃。
所以當(dāng)一些新的數(shù)據(jù)插入到表中時(shí),如果對(duì)這些新的數(shù)據(jù)進(jìn)行分析,是一個(gè)非常重要的問(wèn)題。
一般參考如下原則:
( 1) 看一下新插入的數(shù)據(jù)在全表中所占的比例,如果所占比例不是很大,那么可以考慮不做全局分析,否則就需要考慮,一句是業(yè)務(wù)的實(shí)際運(yùn)行情況。
( 2) 采樣比例。 如果載入的數(shù)據(jù)量非常大,比如上千萬(wàn)或者更大,就要把采樣比例壓縮的盡可能地小,但底線是不能影響 CBO做出正確的執(zhí)行計(jì)劃,采樣比例的上線是不能消耗太多的資源而影響到業(yè)務(wù)的正常運(yùn)行。
( 3) 新加載的數(shù)據(jù)應(yīng)該要做分區(qū)級(jí)的數(shù)據(jù)分析。 至于是否需要直方圖分析,以及設(shè)置多少個(gè) buckets( size 參數(shù)指定),需要 DBA依據(jù)數(shù)據(jù)的分布情況進(jìn)行考慮,關(guān)鍵是視數(shù)據(jù)的傾斜程度而定。
GATHER_SCHEMA_STATS
這個(gè)存儲(chǔ)過(guò)程用于對(duì)某個(gè)用戶下所有的對(duì)象進(jìn)行分析。如果你的數(shù)據(jù)用戶對(duì)象非常多,單獨(dú)對(duì)每個(gè)對(duì)象進(jìn)行分析設(shè)定會(huì)非常不方便,這個(gè)存儲(chǔ)過(guò)程就很方便。
它的好處在于如果需要分析的對(duì)象非常多,將可以大大降低 DBA 的工作量,不足之處是所有分析使用相同的分析策略,可能會(huì)導(dǎo)致分析不是最優(yōu)。 所以要根據(jù)實(shí)際情況來(lái)決定。
GATHER_INDEX_STATS
該 存 儲(chǔ) 過(guò) 程 用 于 對(duì) 索 引 的 分 析 , 如 果 我 們 在 使 用
DBMS_STATS.GATHER_TABLES_STATS 的分析時(shí)設(shè)置參數(shù) cascade=>true。
那么 Oracle 會(huì)同時(shí)執(zhí)行這個(gè)存儲(chǔ)過(guò)程來(lái)對(duì)索引進(jìn)行分析。
DBMS_STATS 包管理功能
獲取分析數(shù)據(jù)
GET_COLUMN_STATS Procedures
GET_INDEX_STATS Procedures
GET_SYSTEM_STATS Procedure
GET_TABLE_STATS Procedure
這四個(gè)存儲(chǔ)過(guò)程分別為用戶獲取字段,索引,表和系統(tǒng)的統(tǒng)計(jì)信息。 它的用法是首先定義要獲取性能指標(biāo)的變量,然后使用存儲(chǔ)過(guò)程將性能指標(biāo)的值賦給變量,最后將變量的值輸出。
設(shè)置分析數(shù)據(jù)
SET_COLUMN_STATS Procedures
SET_INDEX_STATS Procedures
SET_SYSTEM_STATS Procedure
SET_TABLE_STATS Procedure
這幾個(gè)存儲(chǔ)過(guò)程允許我們手工地為字段,索引,表和系統(tǒng)性能數(shù)據(jù)賦值。 它的一個(gè)用處是當(dāng)相應(yīng)的指標(biāo)不準(zhǔn)確導(dǎo)致執(zhí)行計(jì)劃失敗時(shí),可以使用這種方法手工地來(lái)為這些性能數(shù)據(jù)賦值。 在極端情況下,這也不失為一個(gè)解決問(wèn)題的方法。
SET_TABLE_STATS 實(shí)例演示:
SQL> select count(1) from t; COUNT(1) ---------- 33872SQL> exec dbms_stats.set_table_stats(user,'t',numrows => 100000);PL/SQL procedure successfully completed##執(zhí)行sql SQL>select * from t; .....省略輸出 SQL> select a.SQL_ID ,a.CHILD_NUMBER,a.SQL_TEXT from v$sql a where a.SQL_TEXT like 'select * from t ';SQL_ID CHILD_NUMBER SQL_TEXT ------------- ------------ -------------------------------------------------------------------------------- ckzaraqcmkr2f 0 select * from t SQL> select * from table(dbms_xplan.display_cursor('ckzaraqcmkr2f',0));PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- SQL_ID ckzaraqcmkr2f, child number 0 ------------------------------------- select * from t Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 18 (100)| | | 1 | TABLE ACCESS FULL| T | 100K| 390K| 18 (6)| 00:00:01 | --------------------------------------------------------------------------13 rows selectedSQL>可以看到,通過(guò)這種方式,我們輕松地改變了T表的統(tǒng)計(jì)信息,T表中本來(lái)有3W多條記錄,通過(guò)dbms_stats.set_table_stats將表的記錄更改為100000條。這樣,在執(zhí)行計(jì)劃中,CBO得到表上的記錄數(shù)為100000條,銅鼓哦這種方式可以非常容易的模擬出我們希望的各種性能數(shù)據(jù)
刪除分析數(shù)據(jù)
DELETE_COLUMN_STATS Procedure
DELETE_DATABASE_STATS Procedure
DELETE_DICTIONARY_STATS Procedure
DELETE_FIXED_OBJECTS_STATS Procedure
DELETE_INDEX_STATS Procedure
DELETE_SCHEMA_STATS Procedure
DELETE_SYSTEM_STATS Procedure
DELETE_TABLE_STATS Procedure
當(dāng)性能數(shù)據(jù)出現(xiàn)異常導(dǎo)致 CBO 判斷錯(cuò)誤時(shí),為了立刻修正這個(gè)錯(cuò)誤,刪除性能數(shù)據(jù)也是一種補(bǔ)救的方法,比如刪除表的數(shù)據(jù),讓 CBO 重新對(duì)表做動(dòng)態(tài)采樣分析,得到一個(gè)正確的結(jié)果。
它可以刪除字段,數(shù)據(jù)庫(kù),數(shù)據(jù)字典,基表,索引,表等級(jí)別的性能數(shù)據(jù)。另外這也為我們模擬各種測(cè)試環(huán)境提供了一種便捷的方式
SQL> exec dbms_stats.delete_table_stats(user,'t');PL/SQL procedure successfully completedSQL>保存分析數(shù)據(jù)
CREATE_STAT_TABLE Procedure
DROP_STAT_TABLE Procedure
可以用這兩個(gè)存儲(chǔ)過(guò)程創(chuàng)建一個(gè)表,用于存放性能數(shù)據(jù),這樣有利于對(duì)性能數(shù)據(jù)的管理,也可以刪除這個(gè)表。
導(dǎo)入和導(dǎo)出分析數(shù)據(jù)
EXPORT_COLUMN_STATS Procedure
EXPORT_DATABASE_STATS Procedure
EXPORT_DICTIONARY_STATS Procedure
EXPORT_FIXED_OBJECTS_STATS Procedure
EXPORT_INDEX_STATS Procedure
EXPORT_SCHEMA_STATS Procedure
EXPORT_SYSTEM_STATS Procedure
EXPORT_TABLE_STATS Procedure
IMPORT_COLUMN_STATS Procedure
IMPORT_DATABASE_STATS Procedure
IMPORT_DICTIONARY_STATS Procedure
IMPORT_FIXED_OBJECTS_STATS Procedure
IMPORT_INDEX_STATS Procedure
IMPORT_SCHEMA_STATS Procedure
IMPORT_SYSTEM_STATS Procedure
IMPORT_TABLE_STATS Procedure
這些存儲(chǔ)過(guò)程可以將已經(jīng)有的性能指標(biāo)導(dǎo)入到用戶創(chuàng)建好的表中存放,需要時(shí),可以從表中倒回來(lái)。
鎖定分析數(shù)據(jù)
LOCK_SCHEMA_STATS Procedure
LOCK_TABLE_STATS Procedure
UNLOCK_SCHEMA_STATS Procedure
UNLOCK_TABLE_STATS Procedure
可能在某些時(shí)候,我們覺(jué)得當(dāng)前的統(tǒng)計(jì)信息非常好,執(zhí)行計(jì)劃很準(zhǔn)確,并且表中數(shù)據(jù)幾乎不變化,那么可以使用 LOCK_TABLE_STATS Procedure 來(lái)鎖定表的統(tǒng)計(jì)信息,不允許對(duì)表做分析或者設(shè)定分析數(shù)據(jù)。
當(dāng)表的分析數(shù)據(jù)被鎖定之后,相關(guān)的所有分析數(shù)據(jù),包括表級(jí),列級(jí),直方圖,索引的分析數(shù)據(jù)都將被鎖定,不允許被更新。
分析數(shù)據(jù)的恢復(fù)
RESET_PARAM_DEFAULTS Procedure
RESTORE_DICTIONARY_STATS Procedure
RESTORE_FIXED_OBJECTS_STATS Procedure
RESTORE_SCHEMA_STATS Procedure
RESTORE_SYSTEM_STATS Procedure
RESTORE_TABLE_STATS Procedure
比如我們重新分析了表,發(fā)現(xiàn)分析的數(shù)據(jù)導(dǎo)致了 CBO 選擇了錯(cuò)誤的執(zhí)行計(jì)劃,為了挽救這種局面,可以將統(tǒng)計(jì)信息恢復(fù)到從前的那個(gè)時(shí)間點(diǎn),也就是 CBO 執(zhí)行計(jì)劃正確的時(shí)間點(diǎn),先解決這個(gè)問(wèn)題,再來(lái)分析問(wèn)題的原因。
SQL> select dbms_stats.get_stats_history_availability from dual;GET_STATS_HISTORY_AVAILABILITY ------------------------------------------ 08-DEC-16 11.13.35.798721000 PM +08:00SQL>單表分析:分區(qū)表:BEGINDBMS_STATS.GATHER_TABLE_STATS(ownname => 'cc',tabname => 't',estimate_percent => 100,no_invalidate => FALSE,--立馬生效degree => 8,granularity => 'ALL', --分區(qū)cascade => TRUE);END;/非分區(qū)表:BEGINDBMS_STATS.GATHER_TABLE_STATS(ownname => 'cc',tabname => 't',estimate_percent => 100,no_invalidate => FALSE,--立馬生效degree => 8,cascade => TRUE);END;
總結(jié)
以上是生活随笔為你收集整理的Oracle优化07-分析及动态采样-DBMS_STATS 包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Oracle优化08-并行执行
- 下一篇: Oracle优化07-分析及动态采样-动