OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.3.1

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.3.1
    iconOceanBase Database
    SQL - V 4.3.1
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Manage statistics history

    Last Updated:2026-04-15 08:25:14  Updated
    Share
    What is on this page
    Statistics history tables and views
    Functions for statistics history management
    Strategies of statistics history cleanup
    Examples

    folded

    Share

    The statistics history management feature can save statistics information status to a statistics history table when the statistics status changes, such as being recollected, set, deleted, imported, or locked. In the statistics history table, you can query the historical statistics changes of a table and restore the statistics information of a table to a specified historical version.

    Statistics history tables and views

    The OceanBase Database optimizer uses the following views to store statistics history:

    Applicability

    At present, OceanBase Database Community Edition supports only the DBA_TAB_STATS_HISTORY, DBA_TAB_COL_STATISTICS, and DBA_TAB_HISTOGRAMS views.

    • ALL_TAB_STATS_HISTORY, DBA_TAB_STATS_HISTORY, and USER_TAB_STATS_HISTORY store table-level historical statistics.

    • ALL_TAB_COL_STATISTICS, DBA_TAB_COL_STATISTICS, and USER_TAB_COL_STATISTICS store column-level historical basic statistics.

    • ALL_TAB_HISTOGRAMS, DBA_TAB_HISTOGRAMS, and USER_TAB_HISTOGRAMS store column-level historical histogram statistics.

    The fields of a table are basically the same as the fields in the internal table that stores statistics, except that the internal table has the savtime column, which records the time when the statistics status changes. You can also query the following views for historical changes in statistics.

    View
    Description
    ALL_TAB_STATS_HISTORY, DBA_TAB_STATS_HISTORY, and USER_TAB_STATS_HISTORY You can query these views to check the statistics changes.
    sys.ALL_VIRTUAL_TABLE_STAT_V2_HISTORY_REAL_AGENT You can query this view to check the table-level statistics history.
    sys.ALL_VIRTUAL_COLUMN_STAT_V2_HISTORY_REAL_AGENT You can query this view to check the column-level statistics history.
    sys.ALL_VIRTUAL_HISTOGRAM_STAT_V2_HISTORY_REAL_AGENT You can query this view to check the column-level histogram history.

    Functions for statistics history management

    OceanBase Database supports the following functions for statistics history management:

    • restore_table_stats: restores the table-level statistics to a specified time point.

    • restore_schema_stats: restores the schema-level statistics to a specified time point.

    • purge_stats: deletes historical statistics before a specified time point.

    • alter_stats_history_retention: changes the retention period of historical statistics. The default retention period is 31 days.

    • get_stats_history_retention: obtains the current retention period of historical statistics.

    • get_stats_history_availability: obtains the earliest historical statistics available currently. The statistics cannot be restored to any version earlier than this version.

    The definitions are as follows:

    PROCEDURE restore_table_stats (
      ownname               VARCHAR2,
      tabname               VARCHAR2,
      as_of_timestamp       TIMESTAMP WITH TIME ZONE,
      restore_cluster_index BOOLEAN DEFAULT FALSE,
      force                 BOOLEAN DEFAULT FALSE,
      no_invalidate         BOOLEAN DEFAULT FALSE
    );
    
    PROCEDURE restore_schema_stats (
      ownname               VARCHAR2,
      as_of_timestamp       TIMESTAMP WITH TIME ZONE,
      force                 BOOLEAN DEFAULT FALSE,
      no_invalidate         BOOLEAN DEFAULT FALSE
    );
    
    PROCEDURE purge_stats(
      before_timestamp      TIMESTAMP WITH TIME ZONE
    );
    
    PROCEDURE alter_stats_history_retention(
      retention             NUMBER
    );
    
    FUNCTION get_stats_history_retention RETURN NUMBER;
    
    FUNCTION get_stats_history_availability RETURN TIMESTAMP WITH TIME ZONE;
    

    The following table describes the parameters.

    Parameter
    Description
    ownname
    • Oracle mode: the username. If the username is set to NULL, the current logon username is used by default.
    • MySQL mode: the name of the database to which the table belongs.
    tabname The name of the table.
    as_of_timestamp The timestamp to which statistics are restored.
    restore_cluster_index This parameter is not available.
    force Specifies whether to restore statistics by force and ignore the lock. Default value: False.
    no_invalidate This parameter is not available.

    Strategies of statistics history cleanup

    OceanBase Database supports automatic and manual cleanup of statistics history.

    OceanBase Database is a distributed database. Therefore, you cannot create data cleanup tasks when you create a tenant on the server. You need to use the DBMS_JOB package to create a scheduled task on the tenant created or upgraded from an earlier version to automatically clean up the statistics history. Here is an example:

    ## Set a task to automatically clean up statistics history every other day from now on.
    DECLARE
       jid BINARY_INTEGER;
    BEGIN
       DBMS_JOB.SUBMIT(jid, 'dbms_stats.purge_stats(NULL);', trunc(sysdate), 'trunc(sysdate) + 1');
       COMMIT;
    END;
    

    The default cleanup interval is 31 days. You can use dbms_stats.alter_stats_history_retention() to set the interval. The value range is [0, 365000]. If the value is set to 0, all historical statistics are cleared and no subsequent statistics changes will be recorded.

    To manually clean up statistics history, call the dbms_stats.purge_stats() command.

    Examples

    • Obtain the retention period of the historical statistics.

      SELECT dbms_stats.get_stats_history_retention() FROM DUAL;
      
    • Obtain the time of the earliest available historical statistics.

      SELECT dbms_stats.get_stats_history_availability() FROM DUAL;
      
    • Change the retention period of historical statistics to 15 days.

      CALL dbms_stats.alter_stats_history_retention(15);
      
    • Restore the statistics on table tbl1 under the user test to a specific point in time.

      CALL dbms_stats.restore_schema_stats('test', 'tbl1', to_timestamp('2021-09-26 19:02:12.675729', 'YYYY-MM-DD HH24:MI:SS.FF'));
      
    • Manually purge the historical statistics at the specified point in time.

      CALL dbms_stats.purge_stats(to_timestamp('2021-09-26 19:02:12.675729', 'YYYY-MM-DD HH24:MI:SS.FF'));
      

    Previous topic

    Lock and unlock statistics
    Last

    Next topic

    Manage statistics preferences
    Next
    What is on this page
    Statistics history tables and views
    Functions for statistics history management
    Strategies of statistics history cleanup
    Examples