国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

使用DBMS_STATS來收集統計信息

來源:懂視網 責編:小采 時間:2020-11-09 07:48:05
文檔

使用DBMS_STATS來收集統計信息

使用DBMS_STATS來收集統計信息:overview Oracles cost-based optimizer (COB) uses statistics to calculate the selectivity (the fraction of rows in a table that the SQL statements predicate chooses) of predicates and to estimate the cost of each execution plan. The COB wil
推薦度:
導讀使用DBMS_STATS來收集統計信息:overview Oracles cost-based optimizer (COB) uses statistics to calculate the selectivity (the fraction of rows in a table that the SQL statements predicate chooses) of predicates and to estimate the cost of each execution plan. The COB wil

Index Statistics

Oracle collects the following statistics for an index. Statistics marked with an asterisk are always computed exactly. For conventional indexes, the statistics appear in the data dictionary views USER_INDEXES, ALL_INDEXES, and DBA_INDEXES in the columns in parentheses.(帶*為準確值)

oracle收集如下的索引統計信息。對于常規索引,可以在視圖USER_INDEXES, ALL_INDEXES, and DBA_INDEXES中查看到如下的統計信息。

* Depth of the index from its root block to its leaf blocks (BLEVEL)(從0開始) Number of leaf blocks (LEAF_BLOCKS)(葉子塊的數量) Number of distinct index values (DISTINCT_KEYS) Average number of leaf blocks per index value (AVG_LEAF_BLOCKS_PER_KEY)(每個索引值存在于幾個葉子塊,通常為1) Average number of data blocks per index value (for an index on a table) (AVG_DATA_BLOCKS_PER_KEY)(每個索引值對應的記錄存在于幾個數據塊,通常為1) Clustering factor (how well ordered the rows are about the indexed values) (CLUSTERING_FACTOR)(聚簇因子)

Where are the statistics stored?

Statistics are stored into the Oracle Data Dictionary, in tables owned by SYS. Views are created on these tables to retrieve data more easily.

These views are prefixed with DBA_ or ALL_ or USER_. For ease of reading, we will use DBA_% views, but ALL_% views or USER_% views could be used as well.

統計信息存儲在數據字典中,在sys用戶下的表內。通過視圖我們可以非常方便的從這些表中獲取信息。視圖通常以DBA_ USER_ ALL_開始。為了簡便,我們以DBA_開頭的視圖為例。

Conventions Used

- Statistics available only since 8.0.X rdbms release : (*)
- Statistics available only since 8.1.X rdbms release : (**)
- Statistics not available at partition or subpartition level : (G)
- Statistics not available at subpartition level : (GP)

Table level statistics can be retrieved from:

DBA_ALL_TABLES - (8.X onwards)DBA_OBJECT_TABLES - (8.X onwardsDBA_TABLES - (all versions)DBA_TAB_PARTITIONS - (8.X onwards)DBA_TAB_SUBPARTITIONS - (8.1 onwards)

Columns to look at are:

 NUM_ROWS : Number of rows (always exact even when computed 
 	 with ESTIMATE method) 
 BLOCKS : Number of blocks which have been used even 
 if they are empty due to delete statements 
 EMPTY_BLOCKS : Number of empty blocks (these blocks have 
 never been used) 
 AVG_SPACE : Average amount of FREE space in bytes in blocks 
 allocated to the table : Blocks + Empty Blocks 
 CHAIN_CNT : Number of chained or migrated rows 
 AVG_ROW_LEN : Average length of rows in bytes 
 AVG_SPACE_FREELIST_BLOCKS (*)(G) : Average free space of blocks in the freelist 
 NUM_FREELIST_BLOCKS (*)(G) : Number of blocks in the freelist 
 SAMPLE_SIZE : Sample defined in ESTIMATE method (0 if COMPUTE) 
 LAST_ANALYZED : Timestamp of last analysis 
 GLOBAL_STATS (**) : For partitioned tables, YES means statistics 
 are collected for the TABLE as a whole 
 NO means statistics are estimated from statistics 
 on underlying table partitions or subpartitions 
 USER_STATS (**) : YES if statistics entered directly by the user

Index level statistics can be retrieved from:

DBA_INDEXES - (all versions )DBA_IND_PARTITIONS - (8.X onwards)DBA_IND_SUBPARTITIONS - (8.1 onwards )

Columns to look at are:

 BLEVEL : B*Tree level : depth of the index from its root 
 block to its leaf blocks (從0開始)
 LEAF_BLOCKS : Number of leaf blocks 
 DISTINCT_KEYS : Number of distinct keys 
 AVG_LEAF_BLOCKS_PER_KEY : Average number of leaf blocks in which each 
 distinct key appears (1 for a UNIQUE index) 
 AVG_DATA_BLOCKS_PER_KEY : Average number of data blocks in the table that 
 are pointed to by a distinct key 
 CLUSTERING_FACTOR : - if near the number of blocks, then the table is 
 ordered : index entries in a single leaf block 
 tend to point to rows in same data block 
 - if near the number of rows, the table is 
 randomly ordered : index entries in a single 
 leaf block are unlikely to point to rows in 
 same data block 
 SAMPLE_SIZE : Sample defined in ESTIMATE method (0 if COMPUTE) 
 LAST_ANALYZED : Timestamp of last analysis 
 GLOBAL_STATS (**) : For partitioned indexes, YES means statistics 
 are collected for the INDEX as a whole 
 NO means statistics are estimated from statistics 
 on underlying index partitions or subpartitions 
 USER_STATS (**) : YES if statistics entered directly by the user 
 PCT_DIRECT_ACCESS (**)(GP) : For secondary indexes on IOTs, percentage of 
 rows with VALID guess(可以通過alter index index_name update block references來更新)

Column level statistics can be retrieved from:

DBA_TAB_COLUMNS - (all versions)DBA_TAB_COL_STATISTICS - (Version 8.X onwards)DBA_PART_COL_STATISTICS - (Version 8.X onwards)DBA_SUBPART_COL_STATISTICS - (Version 8.1 onwards)

The last three views extract statistics data from DBA_TAB_COLUMNS.(后三個視圖是從DBA_TAB_COLUMNS獲取數據)

Columns to look at are:

 NUM_DISTINCT : Number of distinct values 
 LOW_VALUE : Lowest value 
 LOW_VALUE : Highest value 
 DENSITY : Density 
 NUM_NULLS : Number of columns having a NULL value 
 AVG_COL_LEN : Average length in bytes 
 NUM_BUCKETS : Number of buckets in histogram for the column 
 SAMPLE_SIZE : Sample defined in ESTIMATE method (0 if COMPUTE) 
 LAST_ANALYZED : Timestamp of last analysis 
 (**)GLOBAL_STATS : For partitioned tables, YES means statistics 
 are collected for the TABLE as a whole 
 NO means statistics are estimated from statistics 
 on underlying table partitions or subpartitions 
 (**)USER_STATS : YES if statistics entered directly by the user

Compute statistics vs. Estimate statistics

Both computed and estimated statistics are used by the Oracle optimizer to choose the execution plan for SQL statements that access analyzed objects. These statistics may also be useful to application developers who write such statements.

無論是采用compute還是采用estimat的方式計算統計信息,優化器都會根據這些信息來選擇執行計劃。程序員也可以根據這些統計信息來編寫sql語句。

COMPUTE STATISTICS

COMPUTE STATISTICS instructs Oracle to compute exact statistics about the analyzed object and store them in the data dictionary.

When computing statistics, an entire object is scanned to gather data about the object. This data is used by Oracle to compute exact statistics about the object. Slight variances throughout the object are accounted for in these computed statistics. Because an entire object is scanned to gather information for computed statistics, the larger the size of an object, the more work that is required to gather the necessary information.

To perform an exact computation, Oracle requires enough space to perform a scan and sort of the table. If there is not enough space in memory, then temporary space may be required. For estimations, Oracle requires enough space to perform a scan and sort of only the rows in the requested sample of the table. For indexes, computation does not take up as much time or space, so it is best to perform a full computation.

Some statistics are always computed exactly, such as the number of data blocks currently containing data in a table or the depth of an index from its root block to its leaf blocks.

Use estimation for tables and clusters rather than computation, unless you need exact values. Because estimation rarely sorts, it is often much faster than computation, especially for large tables.

當COMPUTE STATISTICS時,oracle會精確計算被分析對象的統計信息,并將其存儲在數據字典中。oracle會掃描整個對象來獲取數據,并根據這些數據計算統計信息。對于這種方式,基本是輕微的變化也會被計算在內。因為整個對象都會被掃描,因此對象越大就會需要越多的工作量來完成統計。

為了完成精確統計,oracle需要足夠的空間來執行掃描和排序作業。如果在內存中不存在足夠的空間,就會占用磁盤的臨時空間。對于estimation方式,oracle僅僅需要掃描和排序所采樣的內容。如果我們統計的對象是索引,computation方式不會占用太多的時間和空間,因此對于索引我們最好采用compute方式。

某些統計信息總是精確計算的,例如表所占用的數據塊數量和索引的深度。

對于表和聚簇,我們建議使用estimation的方式,除非真的需要精確的統計信息。因此estatimation方式通常不會發生排序,速度更快,尤其在分析大表時。

ESTIMATE STATISTICS

ESTIMATE STATISTICS instructs Oracle to estimate statistics about the analyzed object and stores them in the data dictionary.

When estimating statistics, Oracle gathers representative information from portions of an object. This subset of information provides reasonable, estimated statistics about the object. The accuracy of estimated statistics depends upon how representative the sampling used by Oracle is. Only parts of an object are scanned to gather information for estimated statistics, so an object can be analyzed quickly. You can optionally specify the number or percentage of rows that Oracle should use in making the estimate.

estimate statistics 使得oracle評估待分析對象的統計信息并將它們存儲在數據字典中。當評估統計信息時,oracle在待分析對象的部分區間內收集信息。這部分信息為分析對象提供了足夠的內容。estimate方式的準確程度主要依賴于oracle是如何采樣的。由于只有部分內容被掃描,因此速度更快。我們可以指定oracle采樣的百分比。

To estimate statistics, Oracle selects a random sample of data. You can specify the sampling percentage and whether sampling should be based on rows or blocks.

對于estimate方式,oracle會隨機采樣數據。我們可以指定采樣的百分比,也可以指定是根據記錄還是根據塊來采樣。

Row sampling reads rows without regard to their physical placement on disk. This provides the most random data for estimates, but it can result in reading more data than necessary. For example, in the worst case a row sample might select one row from each block, requiring a full scan of the table or index.基于記錄的采用不會考慮記錄的物理存儲位置。這種方式提供了更好的隨機性,但是可能會造成讀取更多的數據。在最壞的情況下,oracle可能會在每個數據塊中讀取一條記錄,從而會全表掃描表或者索引Block sampling reads a random sample of blocks and uses all of the rows in those blocks for estimates. This reduces the amount of I/O activity for a given sample size, but it can reduce the randomness of the sample if rows are not randomly distributed on disk. Block sampling is not available for index statistics.基于塊的采樣會隨機讀取數據塊,然后利用數據塊中的所有記錄來進行分析統計工作。這無疑減少了輸入輸出的數量,但是如果記錄在塊內的分布不是隨機的,這種方式會影響采樣的隨機性。對于索引,基于塊的采樣方式是不可用的。

Notes on estimating statistics

The default estimate of the analyze command reads the first approx 1064 rows of the table so the results often leave a lot to be desired.默認情況下,oracle會讀取表中的前1064條記錄來作為采樣數據。 The general consensus is that the default value of 1064 is not sufficient for accurate statistics when dealing with tables of any size. Many claims have shown that estimating statistics on 30 percent produces very accurate results. I personally have been running estimate 35 percent. This seems to produce very accurate numbers. It also saves a lot of time over full scans.通常情況下,默認采樣1064條記錄是不充分的。多數人認為30%的采樣會產生比較準確的結果。我個人常常將采樣比例設置為35% Note that if an estimate does 50% or more of a table Oracle converts the estimate to a full compute statistics.如果采樣比超過50%,oracle會將其轉換為full compute statiistics

DBMS_STATS functions and variable definitions

Most of the DBMS_STATS procedures include the three parameters statown, stattab, and statid. These parameters allow you to store statistics in your own tables (outside of the dictionary), which does not affect the optimizer. Therefore, you can maintain and experiment with sets of statistics.

大部分DBMS_STAT過程包含三個參數STATOWN,STATTAB和statid。這些參數允許我們將統計信息存放到自己的表中,這些統計信息不回影響優化器。因此,我們可以維護和測試統計信息。

The stattab parameter specifies the name of a table in which to hold statistics, and it is assumed that it resides in the same schema as the object for which statistics are collected (unless the statown parameter is specified). Users may create multiple tables with different stattabidentifiers to hold separate sets of statistics.stattab參數規定了保存統計信息的表明,通常情況下,如果沒有指定statown參數,oracle以被統計對象所在的模式用戶為stattab的擁有者。我們可以使用不同的stattab來分別存儲不同的統計信息。

Additionally, users can maintain different sets of statistics within a single stattab by using the statid parameter, which can help avoid cluttering the user's schema.

靈位,我們也可以指定statid參數,從而在相同的stattab中存儲不同的統計信息,這樣可以使用戶模式顯得井井有條。

For all of the SET or GET procedures, if stattab is not provided (i.e., NULL), then the operation works directly on the dictionary statistics; therefore, users do not need to create these statistics tables if they only plan to modify the dictionary directly. However, if stattab is not NULL, then the SET or GET operation works on the specified user statistics table, and not the dictionary.

對于所有的set和get過程,如果我們沒有指定stattab,oracle會將統計信息寫入數據字典,如果指定了stattab,orcle只會將統計信息寫入用戶自定義表,而不會更新數據字典。

Create Stats Table

DBMS_STATS.CREATE_STAT_TABLE (
 ownname VARCHAR2, 
 stattab VARCHAR2,
 tblspace VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

stattab : Name of the table to create. This value should be passed as the stattab parameter to other procedures when the user does not want to modify the dictionary statistics directly. tblspace : Tablespace in which to create the stat tables. If none is specified, then they are created in the user's default tablespace.

Drop Stats Table

DBMS_STATS.drop_stat_table (
 ownname VARCHAR2, 
 stattab VARCHAR2);
ownname : Name of the schema.

stattab : User stat table identifier.

Gather Schema Stats (本人在測試過程中,即便指定了stattab,該過程依然更新了數據字典)

DBMS_STATS.gather_schema_stats (
 ownname VARCHAR2,
 estimate_percent NUMBER DEFAULT NULL, 
 block_sample BOOLEAN DEFAULT FALSE,
 method_opt VARCHAR2 DEFAULT 'FOR ALL COLUMNS SIZE 1',(size 1 指在該列上不創建histogram,如果該值大于1,則創建histogram)
 degree NUMBER DEFAULT NULL,
 granularity VARCHAR2 DEFAULT 'DEFAULT', 
 cascade BOOLEAN DEFAULT FALSE,
 stattab VARCHAR2 DEFAULT NULL, 
 statid VARCHAR2 DEFAULT NULL,
 options VARCHAR2 DEFAULT 'GATHER', 
 objlist OUT ObjectTab,
 statown VARCHAR2 DEFAULT NULL);
ownname : Schema to analyze (NULL means current schema).

estimate_percent : Percentage of rows to estimate (NULL means compute): The valid range is [0.000001,100).

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 (the phrase 'SIZE 1' is required to ensure gathering statistics in parallel and for use with the phrase hidden):

FOR ALL [INDEXED | HIDDEN] COLUMNS [SIZE integer]

This value is passed to all of the individual tables.

degree : Degree of parallelism (NULL means use table default value).

granularity : Granularity of statistics to collect (only pertinent if the table is partitioned).

DEFAULT: Gather global- and partition-level statistics.SUBPARTITION: Gather subpartition-level statistics.PARTITION: Gather partition-level statistics.GLOBAL: Gather global statistics.ALL: Gather all (subpartition, partition, and global) statistics.

cascade : Gather statistics on the indexes as well.

Index statistics gathering is not parallelized. Using this option is equivalent to running the gather_index_stats procedure on each of the indexes in the schema in addition to gathering table and column statistics.

stattab : User stat table identifier describing where to save the current statistics.

statid : Identifier (optional) to associate with these statistics within stattab.

options : Further specification of which objects to gather statistics for:

GATHER: Gather statistics on all objects in the schema.GATHER STALE: Gather statistics on stale objects as determined by looking at the *_tab_modifications views. Also, return a list of objects found to be stale.GATHER EMPTY: Gather statistics on objects which currently have no statistics. also, return a list of objects found to have no statistics.LIST STALE: Return list of stale objects as determined by looking at the *_tab_modifications views.LIST EMPTY: Return list of objects which currently have no statistics.

objlist : List of objects found to be stale or empty.

statown : Schema containing stattab (if different than ownname).

Export Schema Stats(從數據字典導出到用戶表)

DBMS_STATS.export_schema_stats (
 ownname VARCHAR2,
 stattab VARCHAR2, 
 statid VARCHAR2 DEFAULT NULL,
 statown VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

stattab : User stat table identifier describing where to store the statistics.

statid : Identifier (optional) to associate with these statistics within stattab.

statown : Schema containing stattab (if different than ownname).

Import Schema Stats(從用戶表導入到數據字典)

DBMS_STATS.import_schema_stats (
 ownname VARCHAR2,
 stattab VARCHAR2, 
 statid VARCHAR2 DEFAULT NULL,
 statown VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

stattab : User stat table identifier describing from where to retrieve the statistics.

statid : Identifier (optional) to associate with these statistics within stattab.

statown : Schema containing stattab (if different than ownname).

Delete Schema Stats

DBMS_STATS.delete_schema_stats (
 ownname VARCHAR2, 
 stattab VARCHAR2 DEFAULT NULL,
 statid VARCHAR2 DEFAULT NULL,
 statown VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

stattab : User stat table identifier describing from where to delete the statistics. If stattab is NULL, then the statistics are deleted directly in the dictionary.

statid : Identifier (optional) to associate with these statistics within stattab (Only pertinent if stattab is not NULL).

statown : Schema containing stattab (if different than ownname).

Set Table Stats

DBMS_STATS.set_table_stats (
 ownname VARCHAR2, 
 tabname VARCHAR2, 
 partname VARCHAR2 DEFAULT NULL,
 stattab VARCHAR2 DEFAULT NULL, 
 statid VARCHAR2 DEFAULT NULL,
 numrows NUMBER DEFAULT NULL, 
 numblks NUMBER DEFAULT NULL,
 avgrlen NUMBER DEFAULT NULL, 
 flags NUMBER DEFAULT NULL,
 statown VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

tabname : Name of the table.

partname : Name of the table partition in which to store the statistics. If the table is partitioned and partname is NULL, then the statistics are stored at the global table level.

stattab : User stat table identifier describing where to store the statistics. If stattab is NULL, then the statistics are stored directly in the dictionary.

statid : Identifier (optional) to associate with these statistics within stattab (Only pertinent if stattab is not NULL).

numrows : Number of rows in the table (partition).

numblks : Number of blocks the table (partition) occupies.

avgrlen : Average row length for the table (partition).

flags : For internal Oracle use (should be left as NULL).

statown : Schema containing stattab (if different than ownname).

Get Table Stats

DBMS_STATS.get_table_stats (
 ownname VARCHAR2, 
 tabname VARCHAR2, 
 partname VARCHAR2 DEFAULT NULL,
 stattab VARCHAR2 DEFAULT NULL, 
 statid VARCHAR2 DEFAULT NULL,
 numrows OUT NUMBER, 
 numblks OUT NUMBER,
 avgrlen OUT NUMBER,
 statown VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

tabname : Name of the table to which this column belongs.

partname : Name of the table partition from which to get the statistics. If the table is partitioned and if partname is NULL, then the statistics are retrieved from the global table level.

stattab : User stat table identifier describing from where to retrieve the statistics. If stattab is NULL, then the statistics are retrieved directly from the dictionary.

statid : Identifier (optional) to associate with these statistics within stattab (Only pertinent if stattab is not NULL).

numrows : Number of rows in the table (partition).

numblks : Number of blocks the table (partition) occupies.

avgrlen : Average row length for the table (partition).

statown : Schema containing stattab (if different than ownname).

Get Index Stats

DBMS_STATS.GET_INDEX_STATS (
 ownname VARCHAR2, 
 indname VARCHAR2,
 partname VARCHAR2 DEFAULT NULL,
 stattab VARCHAR2 DEFAULT NULL, 
 statid VARCHAR2 DEFAULT NULL,
 numrows OUT NUMBER, 
 numlblks OUT NUMBER,
 numdist OUT NUMBER, 
 avglblk OUT NUMBER,
 avgdblk OUT NUMBER, 
 clstfct OUT NUMBER,
 indlevel OUT NUMBER,
 statown VARCHAR2 DEFAULT NULL);
ownname : Name of the schema.

indname : Name of the index.

partname : Name of the index partition for which to get the statistics. If the index is partitioned and if partname is NULL, then the statistics are retrieved for the global index level.

stattab : User stat table identifier describing from where to retrieve the statistics. If stattab is NULL, then the statistics are retrieved directly from the dictionary.

statid : Identifier (optional) to associate with these statistics within stattab (Only pertinent if stattab is not NULL).

numrows : Number of rows in the index (partition).

numlblks : Number of leaf blocks in the index (partition).

numdist : Number of distinct keys in the index (partition).

avglblk : Average integral number of leaf blocks in which each distinct key appears for this index (partition).

avgdblk : Average integral number of data blocks in the table pointed to by a distinct key for this index (partition).

clstfct : Clustering factor for the index (partition).

indlevel : Height of the index (partition).

statown : Schema containing stattab (if different than ownname).

Automated table monitoring and stale statistics gathering example

在oracle10g中 statistics_level 初始化參數作為一個全局設置影響對表的監控操作,本文下面涉及的alter_schema_tab_monitoring已經不再被使用,但是到我們調用這些過程時,不會報錯,只是沒有任何事情發生。

You can automatically gather statistics or create lists of tables that have stale or no statistics.

To automatically gather statistics, run the DBMS_STATS.GATHER_SCHEMA_STATS and DBMS_STATS.GATHER_DATABASE_STATS procedures with the OPTIONS and objlist parameters. Use the following values for the options parameter:

GATHER STALE : Gathers statistics on tables with stale statistics.(通過*_tab_modifications視圖)

GATHER : Gathers statistics on all tables. (default)

GATHER EMPTY : Gathers statistics only on tables without statistics.

LIST STALE : Creates a list of tables with stale statistics.(通過*_tab_modifications視圖)

LIST EMPTY : Creates a list of tables that do not have statistics.

The objlist parameter identifies an output parameter for the LIST STALE and LIST EMPTY options. The objlist parameter is of type DBMS_STATS.OBJECTTAB.

Step 1 : Perform a quick analyze to load in base statistics

BEGIN
DBMS_STATS.GATHER_SCHEMA_STATS (
 ownname => 'scott',
 estimate_percent => null, -- Small table, lets compute
 block_sample => false,
 method_opt => 'FOR ALL COLUMNS',
 degree => null, -- No parallelism used in this example
 granularity => 'ALL',
 cascade => true, -- Make sure we include indexes
 options => 'GATHER' -- Gather mode
 );
END;
/

PL/SQL procedure successfully completed.

Step 2 : Examine the current statistics

SELECT table_name, num_rows, blocks, avg_row_len 
FROM user_tables
WHERE table_name='EMP';

TABLE_NAME NUM_ROWS BLOCKS AVG_ROW_LEN
------------------------------ ---------- ---------- -----------
EMP 1500 28 92

Step 3 : Turn on Automatic Monitoring

Now turn on automatic monitoring for the emp table. This can be done using the alter table method. Starting with Oracle 9i, you can also perform this at the "schema", and "entire database" level. I provide the syntax for all three methods below.

通過alter table 語句我們可以設置oracle數據庫自動監控某張表的變化,從9i開始,我們還可以在schema或者數據庫級別設置是否監控數據變化,監控結構會存儲在*_tab_modifications視圖中。

Monitor only the EMP table.

alter table emp monitoring;

Table altered.
Monitor all of the tables within Scott's schema. (Oracle 9i and higher)
BEGIN
 DBMS_STATS.alter_schema_tab_monitoring('scott', true);
END;
/

PL/SQL procedure successfully completed.
Monitor all of the tables within the database. (Oracle 9i and higher)

Note: Although the option to collect statistics for SYS tables is available via ALTER_DATABASE_TAB_MONITORING, Oracle continues to recommend against this practice until the next major release after 9i Release 2. Also note that the ALTER_DATABASE_TAB_MONITORING procedure in the DBMS_STATS package only monitors tables; there is an ALTER INDEX...MONITORING statement which can be used to monitor indexes. Thanks to Nabil Nawaz for providing this and pointing out an error I made in the previous version of this article.

BEGIN
 DBMS_STATS.alter_database_tab_monitoring (
 monitoring => true,
 sysobjs => false); -- Don't set to true, see note above.
END;
/

PL/SQL procedure successfully completed.

Step 4 : Verify that monitoring is turned on.

Note: The results of the following query are from running the alter table ... statement on the emp table only.

可以通過*_tables視圖的monitoring字段來判斷某張表是否開啟了自動監控

SELECT table_name, monitoring
FROM user_tables
ORDER BY monitoring;

TABLE_NAME MONITORING
------------------------------ ----------
DEPT NO
EMP YES

Step 5 : Delete some rows from the database.

SQL> DELETE FROM emp WHERE rownum < 501;

500 rows deleted.

SQL> commit;

Commit complete.

Step 6 : Wait until the monitered data is flushed.

Data can be flushed in several ways. In Oracle 8i, you can wait it out for 3 hours.In Oracle 9i and higher, you only need to wait 15 minutes.In either version, restart the database.For immediate results in Oracle 9i and higher, use the DBMS_STATS.flush_database_monitoring_info package. OK, I'm impatient...
exec dbms_stats.flush_database_monitoring_info;

PL/SQL procedure successfully completed.

Step 7 : Check for what it has collected.

As user "scott", check USER_TAB_MODIFICATIONS to see what it was collected.
SELECT * FROM user_tab_modifications;

TABLE_NAME PARTITION_NAME SUBPARTITION_NAME INSERTS UPDATES DELETES TIMESTAMP TRUNCATED
---------- -------------- ----------------- ------- ------- ------- --------- ---------
EMP 0 0 500 18-SEP-02 NO

Step 8 : Execute DBMS_STATS to gather stats on all "stale" tables.

BEGIN
 DBMS_STATS.GATHER_SCHEMA_STATS(
 ownname => 'scott',
 estimate_percent => null,
 block_sample => false,
 method_opt => 'FOR ALL COLUMNS',
 degree => null,
 granularity => 'ALL',
 cascade => true,
 options => 'GATHER STALE');
END;
/

PL/SQL procedure successfully completed.

Step 9 : Verify that the table is no longer listed in USER_TAB_MODIFICATIONS.

SQL> SELECT * FROM user_tab_modifications;

no rows selected.

Step 10 : Examine some of new statistics collected.

SELECT table_name, num_rows, blocks, avg_row_len 
FROM user_tables where table_name='EMP';

TABLE_NAME NUM_ROWS BLOCKS AVG_ROW_LEN
------------------------------ ---------- ---------- -----------
EMP 1000 28 92

How to determine if dictionary statistics are RDBMS-generated or user-defined

The following section explains how to determine if your dictionary statistics are RDBMS-generated or set by users through one of the DBMS_STATS.SET_xx_STATS procedures.

This is crucial for development environments that are testing the performance of SQL statements with various sets of statistics. The DBA will need to know if the relying statistics are RDBMS-defined or user-defined.

RDBMS-generated statistics are generated by the following:(我們可以通過如下方式生成統計信息)

ANALYZE SQL commandDBMS_UTILITY.ANALYZE_SCHEMA procedureDBMS_UTILITY.ANALYZE_DATABASE procedureDBMS_DDL.ANALYZE_OBJECT procedure8.1 DBMS_STATS.GATHER_xx_STATS procedures User generated statistics are only done through the use of the DBMS_STATS.SET_xx_STATS procedures(如果我們需要手工設置統計信息,只可以通過dbms_stats包的set_xx_stats過程來實現)

The column USER_STATS from DBA_TABLES, ALL_TABLES, USER_TABLES displays:

YES, when statistics are entered directly by a user.NO, when statistics are generated by RDBMS through an ANALYZE statement(如果USER_STATS字段的值為Yes,則統計信息為手工指定,NO,為通過dbms或者analyze方式系統生成)

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

使用DBMS_STATS來收集統計信息

使用DBMS_STATS來收集統計信息:overview Oracles cost-based optimizer (COB) uses statistics to calculate the selectivity (the fraction of rows in a table that the SQL statements predicate chooses) of predicates and to estimate the cost of each execution plan. The COB wil
推薦度:
標簽: 信息 使用 收集
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 涩五月婷婷 | 国内精品线在线观看 | 亚洲图片欧美日韩 | 欧美激情一区 | 国产精品va在线播放 | 日韩一级精品久久久久 | 全免费a级毛片免费毛视频 热re91久久精品国产91热 | 国产精品123| 久久国产综合精品欧美 | 2020精品极品国产色在线观看 | 天堂一区二区三区精品 | 欧美一区二区三区在线 | 在线精品亚洲欧洲第一页 | 啪啪免费网址 | 日本一二三高清 | 国产精品黄大片观看 | 午夜一区二区三区 | 国产不卡在线 | 亚洲精品影院久久久久久 | 成年人黄国产 | 亚洲欧美日韩高清 | 国产精品一区二区av | 日本a在线 | 精品国产日韩亚洲一区二区 | 亚洲色图第一页 | 久久精品一区二区三区四区 | 亚洲第一页中文字幕 | 国产 日韩 欧美 综合 | 国产精品va在线观看无 | 91在线 | 欧美 | 精品一区二区三区免费毛片爱 | 国产高清免费在线观看 | 日日草视频| 成人区精品一区二区不卡亚洲 | 国产一区二区视频在线 | 97久久精品午夜一区二区 | 欧美成人亚洲高清在线观看 | www.亚洲一区| 精品一区二区三区三区 | 国产成人调教视频在线观看 | 日韩国产综合 |