Update the statistical information of the Oracle table GATHER_TABLE_STATS

Source: Internet
Author: User

The GATHER_TABLE_STATS process of the dbms_stats package is required. The owner and table names must be filled in.

PROCEDURE GATHER_TABLE_STATS
Argument Name Type In/Out Default?
-------------------------------------------------------------------
OWNNAME VARCHAR2 IN
TABNAME VARCHAR2 IN
PARTNAME VARCHAR2 IN DEFAULT
ESTIMATE_PERCENT NUMBER IN DEFAULT
BLOCK_SAMPLE BOOLEAN IN DEFAULT
METHOD_OPT VARCHAR2 IN DEFAULT
DEGREE NUMBER IN DEFAULT
GRANULARITY VARCHAR2 IN DEFAULT
CASCADE BOOLEAN IN DEFAULT
STATTAB VARCHAR2 IN DEFAULT
STATID VARCHAR2 IN DEFAULT
STATOWN VARCHAR2 IN DEFAULT
NO_INVALIDATE BOOLEAN IN DEFAULT
STATTYPE VARCHAR2 IN DEFAULT
FORCE BOOLEAN IN DEFAULT

Exec dbms_stats.gather_table_stats ('Scott ', 'D ');

 

The table statistics are not obtained in real time. So sometimes the data in the table is deleted and cannot be reflected from num_rows in user_tables in real time. At this time, collect the statistical information of the following table.

 

SQL> select table_name, num_rows from user_tables where table_name = 'D ';

TABLE_NAME NUM_ROWS
-------------------------
D 4

SQL> select * from d;

DEPTNO DNAME
--------------------------------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS

SQL> delete from d where deptno = 30;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select * from d;

DEPTNO DNAME
--------------------------------------
10 ACCOUNTING
20 RESEARCH
40 OPERATIONS

SQL> select table_name, num_rows from user_tables where table_name = 'D ';

TABLE_NAME NUM_ROWS
-------------------------
D 4

At this time, the number of rows is still 4. We collect statistics.

SQL> exec dbms_stats.gather_table_stats ('Scott ', 'D ');

PL/SQL procedure successfully completed.

SQL> select table_name, num_rows from user_tables where table_name = 'D ';

TABLE_NAME NUM_ROWS
-------------------------
D 3

 

========================================================== ========================================================== ====================

Restore the deleted data ......

 

SQL> alter session set nls_date_format = 'yyyy-mm-dd hh24: mi: ss ';


Session altered.

SQL>
SQL> select sysdate from dual;

SYSDATE
-------------------
2012-02-28 05:01:49

SQL> select * from d as of timestamp to_timestamp ('2017-02-28 04:50:00 ', 'yyyy-mm-dd hh24: mi: ss') where deptno = 30;

DEPTNO DNAME
--------------------------------------
30 SALES

SQL> insert into d select * from d as of timestamp to_timestamp ('2017-02-28 04:50:00 ', 'yyyy-mm-dd hh24: mi: ss ') where deptno = 30;

1 row created.

SQL> select * from d;

DEPTNO DNAME
--------------------------------------
10 ACCOUNTING
20 RESEARCH
40 OPERATIONS
30 SALES

SQL> commit;

Commit complete.

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.