-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add docs for Analyze Embedded in DDL and new system variable #21031
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
89a00aa
.
AilinKid 3b75acc
Update ddl_embedded_analyze.md
AilinKid c59cb36
.
AilinKid 46e3d4e
Update ddl_embedded_analyze.md
AilinKid 42621e0
Update ddl_embedded_analyze.md
AilinKid b638fdb
Update ddl_embedded_analyze.md
AilinKid a0f8d4f
.
AilinKid 7471ab1
./
AilinKid a930bb9
Update ddl_embedded_analyze.md
AilinKid 690c183
Update system-variables.md
AilinKid 7f4fd37
Update ddl_embedded_analyze.md
AilinKid 155ac1f
Update ddl_embedded_analyze.md
AilinKid 381c0f2
Update ddl_embedded_analyze.md
AilinKid aee921f
.
AilinKid 118454e
.
AilinKid ada22a1
.
AilinKid 0f474dc
Update ddl_embedded_analyze.md
hfxsd 21f9f16
add to toc
hfxsd 9d73d73
Update system-variables.md
hfxsd 4474da7
Update ddl_embedded_analyze.md
AilinKid 714b2a7
Update ddl_embedded_analyze.md
AilinKid 9bfc43b
Update ddl_embedded_analyze.md
AilinKid ab5b411
Standardize SQL syntax in documentation examples
hfxsd 01f0cb5
Update ddl_embedded_analyze.md
hfxsd a57db17
Update ddl_embedded_analyze.md
AilinKid 6839a74
Update ddl_embedded_analyze.md
AilinKid 164f671
Apply suggestions from code review
hfxsd fe97ece
Update system-variables.md
hfxsd 1f934b4
Apply suggestions from code review
hfxsd bb2c97a
Update ddl_embedded_analyze.md
AilinKid 332847c
Update system-variables.md
AilinKid 4db08aa
Apply suggestions from code review
hfxsd 12e9b54
Update ddl_embedded_analyze.md
hfxsd b6914b1
Apply suggestions from code review
hfxsd 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
hfxsd marked this conversation as resolved.
Show resolved
Hide resolved
|
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,134 @@ | ||
| --- | ||
| title: DDL Embedded Analyze 优化 | ||
tangenta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| summary: 本章介绍了对于特定涉及索引创建或者更新 DDL 下的内嵌式 Analyze 的优化,主要包含了 [`ADD INDEX`](/sql-statements/sql-statement-add-index.md) 和 [`MODIFY COLUMN`](/sql-statements/sql-statement-modify-column.md) / [`CHANGE COLUMN`](/sql-statements/sql-statement-change-column.md) | ||
| --- | ||
|
|
||
| # Embedded Analyze | ||
|
|
||
| 本文档介绍可用于 `DDL Embedded Analyze` 的优化,相关变量 [`tidb_stats_update_during_ddl`](/system-variables.md#tidb_stats_update_during_ddl-从-v854-版本开始引入) 可用于控制相关 DDL 在涉及索引数据新建或者 Reorg 时候,是否考虑做内联性 Analyze, 该值默认为 `OFF`。 | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## New Create Index | ||
|
|
||
| 当 `tidb_stats_update_during_ddl` 变量为 `ON` 时,新建索引 [`ADD INDEX`](/sql-statements/sql-statement-add-index.md) 的 DDL,可以在 Reorg 阶段结束之后,内联性发起 Analyze 命令,该命令可以在该新索引 Public 之前,分析相关新建索引的统计信息,然后再完成 DDL。考虑到 Analyze 命令可能会带来一定的耗时,TiDB 取第一次 Reorg 的时间作为内联 Analyze 的超时机制,在相关 timeout 触发之后,`Add Index` 将不再同步等待内联 Analyze 的完成,直接继续推进 Public 该索引,这意味着,后续该新索引的 stats 的就绪将异步等待该 Analyze 的完成。 | ||
|
|
||
| ```sql | ||
| mysql> create table t(a int, b int, c int); | ||
| Query OK, 0 rows affected (0.011 sec) | ||
|
|
||
| mysql> insert into t values(1,1,1),(2,2,2),(3,3,3); | ||
| Query OK, 3 rows affected (0.003 sec) | ||
| Records: 3 Duplicates: 0 Warnings: 0 | ||
|
|
||
| mysql> set @@tidb_stats_update_during_ddl=1; | ||
| Query OK, 0 rows affected (0.001 sec) | ||
|
|
||
| mysql> alter table t add index idx(a,b); | ||
| Query OK, 0 rows affected (0.049 sec) | ||
|
|
||
| mysql> explain select * from t use index(idx); | ||
| +-------------------------------+---------+-----------+--------------------------+------------------+ | ||
| | id | estRows | task | access object | operator info | | ||
| +-------------------------------+---------+-----------+--------------------------+------------------+ | ||
| | IndexLookUp_7 | 3.00 | root | | | | ||
| | ├─IndexFullScan_5(Build) | 3.00 | cop[tikv] | table:t, index:idx(a, b) | keep order:false | | ||
| | └─TableRowIDScan_6(Probe) | 3.00 | cop[tikv] | table:t | keep order:false | | ||
| +-------------------------------+---------+-----------+--------------------------+------------------+ | ||
| 3 rows in set (0.001 sec) | ||
|
|
||
| mysql> show stats_histograms where table_name="t"; | ||
| +---------+------------+----------------+-------------+----------+---------------------+----------------+------------+--------------+-------------+-------------+-----------------+----------------+----------------+---------------+ | ||
hfxsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | Db_name | Table_name | Partition_name | Column_name | Is_index | Update_time | Distinct_count | Null_count | Avg_col_size | Correlation | Load_status | Total_mem_usage | Hist_mem_usage | Topn_mem_usage | Cms_mem_usage | | ||
| +---------+------------+----------------+-------------+----------+---------------------+----------------+------------+--------------+-------------+-------------+-----------------+----------------+----------------+---------------+ | ||
| | test | t | | a | 0 | 2025-10-29 00:07:25 | 3 | 0 | 1 | 1 | allLoaded | 155 | 0 | 155 | 0 | | ||
| | test | t | | idx | 1 | 2025-10-29 00:07:25 | 3 | 0 | 0 | 0 | allLoaded | 182 | 0 | 182 | 0 | | ||
| +---------+------------+----------------+-------------+----------+---------------------+----------------+------------+--------------+-------------+-------------+-----------------+----------------+----------------+---------------+ | ||
| 2 rows in set (0.012 sec) | ||
|
|
||
| mysql> insert into t select * from t; // run multi times. | ||
| Query OK, 3145728 rows affected (6.138 sec) | ||
| Records: 3145728 Duplicates: 0 Warnings: 0 | ||
|
|
||
| mysql> alter table t add index idx_2(a,b,c); | ||
| Query OK, 0 rows affected (19.403 sec) | ||
|
|
||
| mysql> admin show ddl jobs; | ||
| +--------+---------+--------------------------+---------------+----------------------+-----------+----------+-----------+----------------------------+----------------------------+----------------------------+---------+----------------------------------------+ | ||
| | JOB_ID | DB_NAME | TABLE_NAME | JOB_TYPE | SCHEMA_STATE | SCHEMA_ID | TABLE_ID | ROW_COUNT | CREATE_TIME | START_TIME | END_TIME | STATE | COMMENTS | | ||
| +--------+---------+--------------------------+---------------+----------------------+-----------+----------+-----------+----------------------------+----------------------------+----------------------------+---------+----------------------------------------+ | ||
| | 151 | test | t | add index | write reorganization | 2 | 148 | 6291456 | 2025-10-29 00:14:47.181000 | 2025-10-29 00:14:47.183000 | NULL | running | analyzing, txn-merge, max_node_count=3 | | ||
| | 150 | test | t | add index | public | 2 | 148 | 3 | 2025-10-29 00:07:25.492000 | 2025-10-29 00:07:25.494000 | 2025-10-29 00:07:25.534000 | synced | txn-merge, max_node_count=3 | | ||
| +--------+---------+--------------------------+---------------+----------------------+-----------+----------+-----------+----------------------------+----------------------------+----------------------------+---------+----------------------------------------+ | ||
| 11 rows in set (0.001 sec) | ||
| ``` | ||
|
|
||
| 从 `Add Index` 事例来看, 在设置完 `@@tidb_stats_update_during_ddl` 之后的 DDL 运行结束之后,我们可以从之后的 SQL 运行中看到相关 `idx` 索引的统计信息已经被加载到了内存,并且已经被用于 Range 构造。我们从 `show stats_histograms` 语句中可以得到验证,相关索引的统计信息已经被分析已经全部加在加载到了内存中。对于时间较长的 Reorg 过程和 Analyze 过程,我们可以在相关的 DDL Job 状态语句中看到相关索引正在被 `Analyzing` 的字段,该提示表明该 DDL Job 已经处于 stats 收集过程中了。 | ||
|
|
||
| ## Reorg Existed Index | ||
|
|
||
| 当 `tidb_stats_update_during_ddl` 变量为 `ON` 时,Reorg 已经存在的索引 [`MODIFY COLUMN`](/sql-statements/sql-statement-modify-column.md) / [`CHANGE COLUMN`](/sql-statements/sql-statement-change-column.md) 的 DDL,可以在 Reorg 阶段结束之后,内联性发起 Analyze 命令,该命令可以在该新索引 Public 之前,分析相关新建索引的统计信息,然后再完成 DDL。考虑到 Analyze 命令可能会带来一定的耗时,TiDB 取第一次 Reorg 的时间作为内联 Analyze 的超时机制,在相关 timeout 触发之后,`Modify Column` / `Change Column` 将不再同步等待内联 Analyze 的完成,直接继续推进 Public 该索引,这意味着,后续该新索引的 stats 的就绪将异步等待该 Analyze 的完成。 | ||
|
|
||
hfxsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```sql | ||
| mysql> create table s(a int); | ||
| Query OK, 0 rows affected (0.012 sec) | ||
|
|
||
| mysql> insert into s values(1),(2),(3); | ||
| Query OK, 3 rows affected (0.003 sec) | ||
| Records: 3 Duplicates: 0 Warnings: 0 | ||
|
|
||
| mysql> set @@tidb_stats_update_during_ddl=1; | ||
| Query OK, 0 rows affected (0.001 sec) | ||
|
|
||
| mysql> alter table s add index idx(a); | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Query OK, 0 rows affected (0.049 sec) | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| mysql> explain select * from s use index(idx); | ||
| +-----------------------+---------+-----------+-----------------------+-----------------------+ | ||
| | id | estRows | task | access object | operator info | | ||
| +-----------------------+---------+-----------+-----------------------+-----------------------+ | ||
| | IndexReader_6 | 3.00 | root | | index:IndexFullScan_5 | | ||
| | └─IndexFullScan_5 | 3.00 | cop[tikv] | table:s, index:idx(a) | keep order:false | | ||
| +-----------------------+---------+-----------+-----------------------+-----------------------+ | ||
| 2 rows in set (0.002 sec) | ||
|
|
||
| mysql> alter table s modify column a varchar(10); | ||
| Query OK, 0 rows affected (0.056 sec) | ||
|
|
||
| mysql> explain select * from s use index(idx); | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| +-----------------------+---------+-----------+-----------------------+-----------------------+ | ||
| | id | estRows | task | access object | operator info | | ||
| +-----------------------+---------+-----------+-----------------------+-----------------------+ | ||
| | IndexReader_6 | 3.00 | root | | index:IndexFullScan_5 | | ||
| | └─IndexFullScan_5 | 3.00 | cop[tikv] | table:s, index:idx(a) | keep order:false | | ||
| +-----------------------+---------+-----------+-----------------------+-----------------------+ | ||
| 2 rows in set (0.003 sec) | ||
|
|
||
| mysql> show stats_histograms where table_name="s"; | ||
| +---------+------------+----------------+-------------+----------+---------------------+----------------+------------+--------------+-------------+-------------+-----------------+----------------+----------------+---------------+ | ||
| | Db_name | Table_name | Partition_name | Column_name | Is_index | Update_time | Distinct_count | Null_count | Avg_col_size | Correlation | Load_status | Total_mem_usage | Hist_mem_usage | Topn_mem_usage | Cms_mem_usage | | ||
| +---------+------------+----------------+-------------+----------+---------------------+----------------+------------+--------------+-------------+-------------+-----------------+----------------+----------------+---------------+ | ||
| | test | s | | a | 0 | 2025-10-29 00:32:43 | 3 | 0 | 0.5 | 1 | allLoaded | 155 | 0 | 155 | 0 | | ||
| | test | s | | a | 0 | 2025-10-29 00:32:43 | 3 | 0 | 1 | 1 | allLoaded | 158 | 0 | 158 | 0 | | ||
| | test | s | | idx | 1 | 2025-10-29 00:32:43 | 3 | 0 | 0 | 0 | allLoaded | 155 | 0 | 155 | 0 | | ||
| | test | s | | idx | 1 | 2025-10-29 00:32:43 | 3 | 0 | 0 | 0 | allLoaded | 158 | 0 | 158 | 0 | | ||
| +---------+------------+----------------+-------------+----------+---------------------+----------------+------------+--------------+-------------+-------------+-----------------+----------------+----------------+---------------+ | ||
| 4 rows in set (0.010 sec) | ||
|
|
||
| mysql> insert into s select * from s; // run multi times. | ||
| Query OK, 3145728 rows affected (6.138 sec) | ||
| Records: 3145728 Duplicates: 0 Warnings: 0 | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| mysql> alter table s modify column a varchar(5); | ||
| Query OK, 0 rows affected (19.403 sec) | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| mysql> admin show ddl jobs; | ||
| +--------+---------+------------------+---------------+----------------------+-----------+----------+-----------+----------------------------+----------------------------+----------------------------+---------+-----------------------------+ | ||
| | JOB_ID | DB_NAME | TABLE_NAME | JOB_TYPE | SCHEMA_STATE | SCHEMA_ID | TABLE_ID | ROW_COUNT | CREATE_TIME | START_TIME | END_TIME | STATE | COMMENTS | | ||
| +--------+---------+------------------+---------------+----------------------+-----------+----------+-----------+----------------------------+----------------------------+----------------------------+---------+-----------------------------+ | ||
| | 153 | test | s | modify column | write reorganization | 2 | 148 | 12582912 | 2025-10-29 00:26:49.240000 | 2025-10-29 00:26:49.244000 | NULL | running | analyzing | | ||
| | 152 | test | s | modify column | public | 2 | 148 | 18874368 | 2025-10-29 00:24:35.386000 | 2025-10-29 00:24:35.387000 | 2025-10-29 00:25:01.071000 | synced | | | ||
| | 151 | test | s | add index | public | 2 | 148 | 6291456 | 2025-10-29 00:14:47.181000 | 2025-10-29 00:14:47.183000 | 2025-10-29 00:15:06.581000 | synced | txn-merge, max_node_count=3 | | ||
| +--------+---------+------------------+---------------+----------------------+-----------+----------+-----------+----------------------------+----------------------------+----------------------------+---------+-----------------------------+ | ||
| 11 rows in set (0.001 sec) | ||
AilinKid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| 从 `Modify Column` 有损 DDL 示例来看, 在设置完 `@@tidb_stats_update_during_ddl` 之后的相关列类型有损变更 DDL 运行结束之后,我们可以从之后的 SQL 运行中 explain 看到相关 `idx` 索引的统计信息已经被加载到了内存,并且已经被用于 Range 构造。我们从 `show stats_histograms` 语句中可以得到验证,相关索引的统计信息已经被分析已经全部加在加载到了内存中。对于时间较长的 Reorg 过程和 Analyze 过程,我们可以在相关的 DDL Job 状态语句中看到相关索引正在被 `Analyzing` 的字段,该提示表明该 DDL Job 已经处于 stats 收集过程中了。 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.