FLUSH STATS_DELTA New in v8.5.7
FLUSH STATS_DELTA persists the pending statistics delta buffered in TiDB memory to the mysql.stats_meta system table immediately.
When you change data using DML statements (such as INSERT, UPDATE, and DELETE), TiDB records changes to the total row count and modified row count of each affected table, and buffers these changes (called the statistics delta) in the memory of the TiDB node that executes the statements. By default, TiDB persists statistics delta to the mysql.stats_meta system table every 20 * stats-lease (60 seconds by default). For more information, see Automatic update.
Because the statistics health state of tables, the output of SHOW STATS_META, and the scheduling of automatic statistics collection depend on the persisted statistics metadata, FLUSH STATS_DELTA is useful when you need the persisted statistics metadata to reflect recent data changes immediately, such as in testing scenarios that verify optimizer behavior. You do not need to execute FLUSH STATS_DELTA before executing ANALYZE TABLE, because TiDB automatically flushes the pending statistics delta of a table before collecting statistics on it.
Synopsis
- FlushStatsDeltaStmt
- FlushTargetList
- FlushTarget
- TableName
- SchemaWildcard
- GlobalWildcard
- ClusterOption
FlushStatsDeltaStmt ::=
'FLUSH' 'STATS_DELTA' FlushTargetList ClusterOption?
FlushTargetList ::=
FlushTarget (',' FlushTarget)*
FlushTarget ::=
TableName
| SchemaWildcard
| GlobalWildcard
TableName ::=
Identifier ('.' Identifier)?
SchemaWildcard ::=
Identifier '.' '*'
GlobalWildcard ::=
'*' '.' '*'
ClusterOption ::=
'CLUSTER'
Options
- Targets (
FlushTargetList): specifies the tables whose statistics delta you want to flush. You must specify at least one target.table_name: flushes the statistics delta of a specific table in the current database. If you do not select a database, TiDB returns theNo database selectederror.db_name.table_name: flushes the statistics delta of a specific table in the specific database.db_name.*: flushes the statistics delta of all tables in the specific database.*.*: flushes the statistics delta of all tables.
CLUSTER: executes the statement on all TiDB nodes in the cluster. Each TiDB node buffers the statistics delta of the DML statements that it executes. Without this option, TiDB only persists the delta buffered on the TiDB node you are connected to.
Note the following behavior:
- TiDB deduplicates overlapping targets to be flushed. For example, in
FLUSH STATS_DELTA *.*, test.t, thetest.ttarget is ignored because*.*already includes all tables. Similarly, inFLUSH STATS_DELTA test.*, test.t, thetest.ttarget is ignored becausetest.*already includes all tables in thetestdatabase. - For a partitioned table, TiDB persists the statistics delta of the table and all its partitions.
- If a specified database or table does not exist, TiDB returns a warning and skips that target.
Examples
Persist the statistics delta of a single table immediately after data changes:
USE test;
CREATE TABLE t (a INT, b INT);
INSERT INTO t VALUES (1, 1), (2, 2), (3, 3);
FLUSH STATS_DELTA t;
Query OK, 0 rows affected (0.01 sec)
TiDB has now persisted the row count changes of the table to the mysql.stats_meta system table. You can view persisted values using SHOW STATS_META. Note that SHOW STATS_META reads statistics from the memory of the TiDB node you are connected to. Because this TiDB node loads the persisted values every stats-lease (3s by default), the flushed values might appear in the output after a short delay:
SHOW STATS_META WHERE table_name = 't';
+---------+------------+----------------+---------------------+--------------+-----------+-------------------+
| Db_name | Table_name | Partition_name | Update_time | Modify_count | Row_count | Last_analyze_time |
+---------+------------+----------------+---------------------+--------------+-----------+-------------------+
| test | t | | 2026-07-13 15:30:00 | 3 | 3 | NULL |
+---------+------------+----------------+---------------------+--------------+-----------+-------------------+
1 row in set (0.01 sec)
Persist the statistics delta of a table in the current database and every table in the sales database:
FLUSH STATS_DELTA t, sales.*;
Persist the statistics delta of all tables buffered on every TiDB node in the cluster:
FLUSH STATS_DELTA *.* CLUSTER;
Privileges
To execute FLUSH STATS_DELTA, you must have the SELECT privilege on the target objects:
- For
table_nameordb_name.table_name, you need theSELECTprivilege on the target table. - For
db_name.*, you need theSELECTprivilege on the target database. - For
*.*, you need the globalSELECTprivilege.
Unlike other FLUSH statements, FLUSH STATS_DELTA does not require the RELOAD privilege.
MySQL compatibility
FLUSH STATS_DELTA is a TiDB extension to MySQL syntax.