-
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
8
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.
+121
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
26ae651
Create empty translation PR
qiancai a98cd8f
Auto-sync: Update Chinese docs from English PR
github-actions[bot] f2a976d
Update sql-statements/sql-statement-flush-stats-delta.md
qiancai c97b9c8
Update sql-statement-flush-stats-delta.md
qiancai bd4bc81
Update sql-statements/sql-statement-overview.md
qiancai 500a6cf
Update releases/release-8.5.7.md
qiancai 4df19b9
Update releases/release-8.5.7.md
qiancai f70edc5
Update sql-statements/sql-statement-flush-stats-delta.md
qiancai 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,117 @@ | ||
| --- | ||
| 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#统计信息相关系统表) 系统表中。 | ||
|
|
||
| 当执行 `INSERT`、`UPDATE`、`DELETE` 等 DML 语句修改数据时,TiDB 会记录每张受影响的表中总行数和修改行数的变化,并将这些变化(即统计信息 delta)缓存在执行这些语句的 TiDB 节点的内存中。默认情况下,TiDB 每隔 20 * [`stats-lease`](/tidb-configuration-file.md#stats-lease)(默认 60 秒)会将这些 delta 持久化到 `mysql.stats_meta` 系统表中。详情参考[自动更新](/statistics.md#自动更新)。 | ||
|
|
||
| 由于[表的统计信息健康度](/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) 时,无需提前执行 `FLUSH STATS_DELTA`,因为 TiDB 会在收集某张表的统计信息之前,自动刷新该表待持久化的统计信息 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.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
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
补充
stats-lease=0的边界行为当前表述暗示刷新后的值最多延迟一个
stats-lease周期即可显示,但当stats-lease=0时不会自动加载更新的统计信息;用户执行FLUSH STATS_DELTA后仍可能看到旧值,需要显式执行REFRESH STATS。(docs.pingcap.com)As per path instructions,Markdown 文档中的可安全连续修复应提供精确替换文本。
建议直接替换
📝 Committable suggestion
Source: Path instructions