-
Notifications
You must be signed in to change notification settings - Fork 1.2k
statistics: add docs for the FLUSH STATS_DELTA statement #21819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qiancai
wants to merge
2
commits into
pingcap:master
Choose a base branch
from
qiancai:add-flush-stats-delta-doc-23258
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| title: FLUSH STATS_DELTA | ||
| summary: TiDB 数据库中 FLUSH STATS_DELTA 用法概述。 | ||
| --- | ||
|
|
||
| # FLUSH STATS_DELTA <span class="version-mark">于 v8.5.7 和 v9.0.0 新增</span> | ||
|
|
||
| `FLUSH STATS_DELTA` 会立即将 TiDB 内存中缓冲的待持久化统计信息 delta 持久化到 [`mysql.stats_meta`](/mysql-schema/mysql-schema.md#statistics-system-tables) 系统表中。 | ||
|
|
||
| 当你使用 DML 语句(例如 `INSERT`、`UPDATE` 和 `DELETE`)修改数据时,TiDB 会记录每个受影响表的总行数和已修改行数的变化,将这些变化(称为 statistics delta)缓存在执行这些语句的 TiDB 节点内存中,并每隔 20 * [`stats-lease`](/tidb-configuration-file.md#stats-lease)(默认 60 秒)将其持久化到 `mysql.stats_meta` 系统表中。更多信息,参见[自动修改](/statistics.md#automatic-update)。 | ||
|
|
||
| 由于[表的健康状态](/sql-statements/sql-statement-show-stats-healthy.md)、[`SHOW STATS_META`](/sql-statements/sql-statement-show-stats-meta.md) 的输出以及自动统计信息收集的调度都依赖已持久化的统计信息元信息,因此当你需要让已持久化的统计信息元信息立即反映最近的数据变更时,`FLUSH STATS_DELTA` 会很有用,例如在验证优化器行为的测试场景中。你无需在 [`ANALYZE TABLE`](/sql-statements/sql-statement-analyze-table.md) 之前执行此语句,因为 TiDB 会在收集某个表的统计信息之前,自动刷新该表待持久化的 statistics delta。 | ||
|
|
||
| ## 语法概要 | ||
|
|
||
| ```ebnf+diagram | ||
| FlushStatsDeltaStmt ::= | ||
| 'FLUSH' 'STATS_DELTA' FlushTargetList ClusterOption? | ||
|
|
||
| FlushTargetList ::= | ||
| FlushTarget (',' FlushTarget)* | ||
|
|
||
| FlushTarget ::= | ||
| TableName | ||
| | SchemaWildcard | ||
| | GlobalWildcard | ||
|
|
||
| TableName ::= | ||
| Identifier ('.' Identifier)? | ||
|
|
||
| SchemaWildcard ::= | ||
| Identifier '.' '*' | ||
|
|
||
| GlobalWildcard ::= | ||
| '*' '.' '*' | ||
|
|
||
| ClusterOption ::= | ||
| 'CLUSTER' | ||
| ``` | ||
|
|
||
| ## 选项 | ||
|
|
||
| - **Targets (`FlushTargetList`)**:指定要刷新的统计信息 delta 所属表。你必须至少指定一个目标。 | ||
| - `table_name`:刷新当前数据库中某个表的统计信息 delta。如果你未选择数据库,TiDB 会返回 `No database selected` 错误。 | ||
| - `db_name.table_name`:刷新指定数据库中某个表的统计信息 delta。 | ||
| - `db_name.*`:刷新指定数据库中所有表的统计信息 delta。 | ||
| - `*.*`:刷新所有表的统计信息 delta。 | ||
| - **`CLUSTER`**:将该语句广播到集群中的所有 TiDB 节点。每个 TiDB 节点都会缓存在其上执行的 DML 语句所产生的统计信息 delta。如果不指定此选项,TiDB 只会持久化你当前连接的 TiDB 节点上缓冲的 delta。 | ||
|
|
||
| 请注意以下行为: | ||
|
|
||
| - TiDB 会对重叠的目标进行去重。例如,在 `FLUSH STATS_DELTA *.*, test.t` 中,`test.t` 目标会被忽略,因为 `*.*` 已经包含所有表。类似地,在 `FLUSH STATS_DELTA test.*, test.t` 中,`test.t` 目标会被忽略,因为 `test.*` 已经包含 `test` 数据库中的所有表。 | ||
| - 对于分区表,TiDB 会持久化该表及其所有分区的统计信息 delta。 | ||
| - 如果指定的数据库或表不存在,TiDB 会返回一条警告并跳过该目标。 | ||
|
|
||
| ## 示例 | ||
|
|
||
| 在数据变更后立即持久化单个表的统计信息 delta: | ||
|
|
||
| ```sql | ||
| 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 已将该表的行数变化持久化到 `mysql.stats_meta` 系统表中。你可以使用 `SHOW STATS_META` 查看已持久化的值。注意,`SHOW STATS_META` 会从你当前连接的 TiDB 节点内存中读取统计信息。由于该 TiDB 节点会在 [`stats-lease`](/tidb-configuration-file.md#stats-lease)(默认 `3s`)内加载已持久化的值,因此刷新后的值可能会在短暂延迟后出现在输出中: | ||
|
|
||
| ```sql | ||
| 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) | ||
| ``` | ||
|
|
||
| 持久化当前数据库中某个表以及 `sales` 数据库中所有表的统计信息 delta: | ||
|
|
||
| ```sql | ||
| FLUSH STATS_DELTA t, sales.*; | ||
| ``` | ||
|
|
||
| 持久化集群中每个 TiDB 节点上缓冲的所有表的统计信息 delta: | ||
|
|
||
| ```sql | ||
| FLUSH STATS_DELTA *.* CLUSTER; | ||
| ``` | ||
|
|
||
| ## 权限 | ||
|
|
||
| 要执行 `FLUSH STATS_DELTA`,你必须对目标对象具有 `SELECT` 权限:对于 `table_name` 或 `db_name.table_name` 目标,需要具有目标表的 `SELECT` 权限;对于 `db_name.*` 目标,需要具有目标数据库的 `SELECT` 权限;对于 `*.*` 目标,需要具有全局 `SELECT` 权限。与其他 `FLUSH` 语句不同,`FLUSH STATS_DELTA` 不需要 `RELOAD` 权限。 | ||
|
|
||
| ## MySQL 兼容性 | ||
|
|
||
| `FLUSH STATS_DELTA` 是 TiDB 对 MySQL 语法的扩展。 | ||
|
|
||
| ## 另请参阅 | ||
|
|
||
| - [Statistics](/statistics.md) | ||
| - [`SHOW STATS_META`](/sql-statements/sql-statement-show-stats-meta.md) | ||
| - [`ANALYZE TABLE`](/sql-statements/sql-statement-analyze-table.md) | ||
| - [`REFRESH STATS`](/sql-statements/sql-statement-refresh-stats.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Correct the shared automatic-update anchor.
Both links target
#automatic-update, while the supplied Chinese heading is### 自动更新; the new statement page also uses the inaccurate label自动修改.sql-statements/sql-statement-flush-stats-delta.md#L10-L10: change the label to自动更新and target/statistics.md#自动更新.statistics.md#L19-L19: change the self-reference target to/statistics.md#自动更新.📍 Affects 2 files
sql-statements/sql-statement-flush-stats-delta.md#L10-L10(this comment)statistics.md#L19-L19🤖 Prompt for AI Agents