Manage Oracle statistics

Source: Internet
Author: User

Under the CBO (cost-based optimizer mode) condition, the execution plan of SQL statements is determined by the statistical information. If no statistical information is available, the execution plan is determined by dynamic sampling! It can be said that the statistical information is related to whether the SQL Execution Plan is correct. It is a guiding ideology for SQL Execution. The oracle initialization parameter statistics_level controls the level of collected statistics. There are three parameter values:

BASIC: collect BASIC statistical information TYPICAL: collect most statistical information (default settings of the database) ALL: Collect ALL statistical information. Statistical information includes: Row statistical information (user_tables ): number of rows (NUM_ROWS), number of BLOCKS (BLOCKS), average length of rows (AVG_ROW_LEN), and column statistics (user_tab_columns): number of unique values in the column NUM_DISTINCT ), number of NULL values (NUM_NULLS), data distribution (HISTOGRAM), index statistics (user_index): -- number of leaf blocks (LEAF_BLOCKS), level (BLEVEL), clustering factor (CLUSTERING_FACTOR ); the collection time of statistical information is scheduled in the time window!

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1T240F50-0.jpg "border =" 0 "alt =" "/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1T2406254-1.jpg "border =" 0 "alt =" "/>

I. 10 Gb disable automatic statistics collection tasks

 
 
  1. begin 
  2.  DBMS_SCHEDULER.DISABLE('GATHER_STATS_JOB'); 
  3. end; 

Ii. Disable Automatic statistics collection tasks at 11 GB.

 
 
  1. BEGIN 
  2. DBMS_AUTO_TASK_ADMIN.DISABLE( 
  3. client_name => 'auto optimizer stats collection', 
  4. operation => NULL, 
  5. window_name => NULL); 
  6. END; 

Query

 
 
  1. SQL> select client_name ,status from dba_autotask_client; 
  2.  
  3. CLIENT_NAME                                                      STATUS 
  4.  
  5. ---------------------------------------------------------------- -------- 
  6.  
  7. auto optimizer stats collection                                  DISABLED 
  8.  
  9. auto space advisor                                               ENABLED 
  10.  
  11. sql tuning advisor                                               ENABLED 

3. Collect statistical information manually. The sampling speed is 10% and the degree of parallelism is 8. The METHOD_OPT option indicates the index column distribution and generates a histogram.

 
 
  1. BEGIN 
  2. dbms_stats.gather_schema_stats( 
  3. ownname=>'HR', 
  4. METHOD_OPT=>'FOR ALL INDEXED COLUMNS SIZE SKEWONLY', 
  5. CASCADE=>TRUE, 
  6. ESTIMATE_PERCENT=>10, 
  7. DEGREE=>8); 
  8. END; 

4. view the statistical information of a table or index

 
 
  1. select table_name,  
  2.        last_analyzed,  
  3.        num_rows,  
  4.        avg_row_len,  
  5.        row_movement, 
  6.        monitoring 
  7. from dba_tables 
  8. where owner = 'HR'; 
  9.  
  10. select index_name, 
  11.        table_name, 
  12.        blevel, 
  13.        status, 
  14.        clustering_factor, 
  15.        last_analyzed, 
  16.        num_rows, 
  17.        leaf_blocks, 
  18.        distinct_keys, 
  19.        avg_leaf_blocks_per_key 
  20. from dba_indexes 
  21. where owner = 'HR'; 

V. Export Statistics

 
 
  1. begin 
  2.   dbms_stats.create_stat_table(ownname => 'HR', 
  3.   stattab => 'HR_STAT_BAK'); 
  4. end; 
  5.  
  6. begin  
  7.   dbms_stats.export_schema_stats(ownname => 'HR', 
  8.   stattab => 'HR_STAT_BAK', 
  9.   statid => 'N1', 
  10.   statown => 'HR'); 
  11. end; 
  12.  
  13. select * from HR_STAT_BAK; 

6. Delete statistics

 
 
  1. begin 
  2.   dbms_stats.delete_schema_stats(ownname => 'HR'); 
  3. end; 

7. import statistics

 
 
  1. begin 
  2.   dbms_stats.import_schema_stats(ownname => 'HR', 
  3.   stattab => 'HR_STAT_BAK', 
  4.   statid => 'N1', 
  5.   statown => 'HR'); 
  6. end; 

8. Import the HR_STAT_BAK table to other oracle servers through expdp/impdp.

 
 
  1. SQL> exec dbms_stats.upgrade_stat_table ('hr', 'hr _ STAT_BAK ');
  2. The PL/SQL process is successfully completed.
  3.  
  4. SQL> EXEC dbms_stats.import_schema_stats (ownname => 'hr', stattab => 'hr _ STAT_BAK ', statid => 'n1', statown => 'hr ');
  5. The PL/SQL process is successfully completed.

9. Lock and unlock statistics

 
 
  1. 1: Locked statistics
  2. Begin
  3. Dbms_stats.lock_schema_stats (ownname => 'hr ');
  4. End;
  5.  
  6. 2: query which tables or indexes are locked.
  7. Select * from user_tab_statistics where stattype_locked = 'all ';
  8. Select * from user_ind_statistics where stattype_locked = 'all ';
  9.  
  10. 3: If statistics are collected under the locking condition, the following error occurs:
  11. ERROR at line 1:
  12. ORA-20005: object statistics are locked (stattype = ALL)
  13. ORA-06512: at "SYS. DBMS_STATS", line 20337
  14. ORA-06512: at "SYS. DBMS_STATS", line 20360
  15. ORA-06512: at line 1
  16.  
  17. 4: Unlock statistics
  18. Begin
  19. Dbms_stats.lock_schema_stats (ownname => 'hr ');
  20. End;

 

 

This article is from the "Bo Yue" blog and will not be reproduced!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.