Skip to content
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

update doc about show analyze status #9497

Merged
merged 21 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions information-schema/information-schema-analyze-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ summary: 了解 information_schema 表 `ANALYZE_STATUS`。

`ANALYZE_STATUS` 表提供正在执行的收集统计信息的任务以及有限条历史任务记录。

从 TiDB v6.1.0 起,`ANALYZE_STATUS` 表显示集群级别的任务,且 TiDB 重启后仍能看到重启之前的任务记录。在 TiDB v6.1.0 之前,`ANALYZE_STATUS` 仅显示实例级别的任务,且 TiDB 重启后任务记录会被清空。

从 TiDB v6.1.0 起,可以通过系统表 `mysql.analyze_jobs` 查看过去 7 天内的历史任务记录。

{{< copyable "sql" >}}

```sql
Expand All @@ -21,40 +25,48 @@ DESC analyze_status;
| TABLE_SCHEMA | varchar(64) | YES | | NULL | |
| TABLE_NAME | varchar(64) | YES | | NULL | |
| PARTITION_NAME | varchar(64) | YES | | NULL | |
| JOB_INFO | varchar(64) | YES | | NULL | |
| PROCESSED_ROWS | bigint(20) unsigned | YES | | NULL | |
| JOB_INFO | longtext | YES | | NULL | |
| PROCESSED_ROWS | bigint(64) unsigned | YES | | NULL | |
| START_TIME | datetime | YES | | NULL | |
| END_TIME | datetime | YES | | NULL | |
| STATE | varchar(64) | YES | | NULL | |
| FAIL_REASON | longtext | YES | | NULL | |
| INSTANCE | varchar(512) | YES | | NULL | |
| PROCESS_ID | bigint(64) unsigned | YES | | NULL | |
+----------------+---------------------+------+------+---------+-------+
7 rows in set (0.00 sec)
11 rows in set (0.00 sec)
```

{{< copyable "sql" >}}

```sql
SELECT * FROM `ANALYZE_STATUS`;
SELECT * FROM information_schema.analyze_status;
```

```sql
+--------------+------------+----------------+-------------------+----------------+---------------------+----------+
| TABLE_SCHEMA | TABLE_NAME | PARTITION_NAME | JOB_INFO | PROCESSED_ROWS | START_TIME | STATE |
+--------------+------------+----------------+-------------------+----------------+---------------------+----------+
| test | t | | analyze index idx | 2 | 2019-06-21 19:51:14 | finished |
| test | t | | analyze columns | 2 | 2019-06-21 19:51:14 | finished |
| test | t1 | p0 | analyze columns | 0 | 2019-06-21 19:51:15 | finished |
| test | t1 | p3 | analyze columns | 0 | 2019-06-21 19:51:15 | finished |
| test | t1 | p1 | analyze columns | 0 | 2019-06-21 19:51:15 | finished |
| test | t1 | p2 | analyze columns | 1 | 2019-06-21 19:51:15 | finished |
+--------------+------------+----------------+-------------------+----------------+---------------------+----------+
6 rows in set
+--------------+------------+----------------+--------------------------------------------------------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
| TABLE_SCHEMA | TABLE_NAME | PARTITION_NAME | JOB_INFO | PROCESSED_ROWS | START_TIME | END_TIME | STATE | FAIL_REASON | INSTANCE | PROCESS_ID |
+--------------+------------+----------------+--------------------------------------------------------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
| test | t | p1 | analyze table all columns with 256 buckets, 500 topn, 1 samplerate | 0 | 2022-05-27 11:30:12 | 2022-05-27 11:30:12 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze table all columns with 256 buckets, 500 topn, 1 samplerate | 0 | 2022-05-27 11:30:12 | 2022-05-27 11:30:12 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p1 | analyze index idx | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze index idx | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p1 | analyze columns | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze columns | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
+--------------+------------+----------------+--------------------------------------------------------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
6 rows in set (0.00 sec)
```

`ANALYZE_STATUS` 表中列的含义如下:

* `TABLE_SCHEMA`:表所属的数据库的名称。
* `TABLE_NAME`:表的名称。
* `PARTITION_NAME`:分区表的名称。
* `JOB_INFO`:`ANALYZE` 任务的信息。
* `JOB_INFO`:`ANALYZE` 任务的信息。如果分析索引,该信息会包含索引名。当 `tidb_analyze_version =2` 时,该信息会包含采样率等配置项。
* `PROCESSED_ROWS`:已经处理的行数。
* `START_TIME`:`ANALYZE` 任务的开始时间。
* `END_TIME`:`ANALYZE` 任务的结束时间。
* `STATE`:`ANALYZE` 任务的执行状态。其值可以是 `pending`、`running`、`finished` 或者 `failed`。
* `FAIL_REASON`:任务失败的原因。如果执行成功则为 `NULL`。
* `INSTANCE`:执行任务的 TiDB 实例。
* `PROCESS_ID`:执行任务的 process ID。
1 change: 1 addition & 0 deletions mysql-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ aliases: ['/docs-cn/dev/system-tables/system-table-overview/','/docs-cn/dev/refe
* `stats_buckets` 统计信息的桶
* `stats_histograms` 统计信息的直方图
* `stats_meta` 表的元信息,比如总行数和修改数
* `analyze_jobs` 正在执行的统计信息收集任务以及过去 7 天内的历史任务记录

## GC Worker 相关系统表

Expand Down
69 changes: 44 additions & 25 deletions sql-statements/sql-statement-show-analyze-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,59 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-show-analyze-status/']

`SHOW ANALYZE STATUS` 语句提供 TiDB 正在执行的统计信息收集任务以及有限条历史任务记录。

## 语法图
从 TiDB v6.1.0 起,执行 `SHOW ANALYZE STATUS` 语句将显示集群级别的任务,且 TiDB 重启后仍能看到重启之前的任务记录。在 TiDB v6.1.0 之前,执行 `SHOW ANALYZE STATUS` 语句仅显示实例级别的任务,且 TiDB 重启后任务记录会被清空。

**ShowStmt:**
从 TiDB v6.1.0 起,你可以通过系统表 `mysql.analyze_jobs` 查看过去 7 天内的历史记录。

![ShowStmt](/media/sqlgram/ShowStmt.png)
## 语法图

**ShowTargetFilterable:**
```ebnf+diagram
ShowAnalyzeStatusStmt ::= 'SHOW' 'ANALYZE' 'STATUS' ShowLikeOrWhereOpt

![ShowTargetFilterable](/media/sqlgram/ShowTargetFilterable.png)
ShowLikeOrWhereOpt ::= 'LIKE' SimpleExpr | 'WHERE' Expression
```

## 示例

{{< copyable "sql" >}}

```sql
create table t(x int, index idx(x)) partition by hash(x) partition 4;
analyze table t;
show analyze status;
```
mysql> create table t(x int, index idx(x)) partition by hash(x) partitions 2;
Query OK, 0 rows affected (0.69 sec)

```sql
+--------------+------------+----------------+-------------------+----------------+---------------------+----------+
| Table_schema | Table_name | Partition_name | Job_info | Processed_rows | Start_time | State |
+--------------+------------+----------------+-------------------+----------------+---------------------+----------+
| test | t | p1 | analyze columns | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p0 | analyze columns | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p0 | analyze index idx | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p1 | analyze index idx | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p2 | analyze index idx | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p3 | analyze index idx | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p3 | analyze columns | 0 | 2020-05-25 17:23:55 | finished |
| test | t | p2 | analyze columns | 0 | 2020-05-25 17:23:55 | finished |
+--------------+------------+----------------+-------------------+----------------+---------------------+----------+
8 rows in set (0.00 sec)
mysql> set @@tidb_analyze_version = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> analyze table t;
Query OK, 0 rows affected (0.20 sec)

mysql> show analyze status;
+--------------+------------+----------------+-------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
| Table_schema | Table_name | Partition_name | Job_info | Processed_rows | Start_time | End_time | State | Fail_reason | Instance | Process_ID |
+--------------+------------+----------------+-------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
| test | t | p1 | analyze index idx | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze index idx | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p1 | analyze columns | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze columns | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
+--------------+------------+----------------+-------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
4 rows in set (0.01 sec)

mysql> set @@tidb_analyze_version = 2;
Query OK, 0 rows affected (0.00 sec)

mysql> analyze table t;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> show analyze status;
+--------------+------------+----------------+--------------------------------------------------------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
| Table_schema | Table_name | Partition_name | Job_info | Processed_rows | Start_time | End_time | State | Fail_reason | Instance | Process_ID |
+--------------+------------+----------------+--------------------------------------------------------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
| test | t | p1 | analyze table all columns with 256 buckets, 500 topn, 1 samplerate | 0 | 2022-05-27 11:30:12 | 2022-05-27 11:30:12 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze table all columns with 256 buckets, 500 topn, 1 samplerate | 0 | 2022-05-27 11:30:12 | 2022-05-27 11:30:12 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p1 | analyze index idx | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze index idx | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p1 | analyze columns | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
| test | t | p0 | analyze columns | 0 | 2022-05-27 11:29:46 | 2022-05-27 11:29:46 | finished | NULL | 127.0.0.1:4000 | NULL |
+--------------+------------+----------------+--------------------------------------------------------------------+----------------+---------------------+---------------------+----------+-------------+----------------+------------+
6 rows in set (0.00 sec)
```

## MySQL 兼容性
Expand Down
37 changes: 19 additions & 18 deletions statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,29 +330,22 @@ ANALYZE INCREMENTAL TABLE TableName PARTITION PartitionNameList INDEX [IndexName

从 TiDB v6.0 起,TiDB 支持通过 `KILL` 语句终止正在后台运行的 `ANALYZE` 任务。如果发现正在后台运行的 `ANALYZE` 任务消耗大量资源影响业务,你可以通过以下步骤终止该 `ANALYZE` 任务:

1. 执行以下 SQL 语句获得正在执行后台 `ANALYZE` 任务的 TiDB 实例地址和任务 `ID`
1. 执行以下 SQL 语句

{{< copyable "sql" >}}

```sql
SELECT ci.instance as instance, cp.id as id FROM information_schema.cluster_info ci, information_schema.cluster_processlist cp WHERE ci.status_address = cp.instance and ci.type = 'tidb' and cp.info like 'analyze table %' and cp.user = '' and cp.host = '';
SHOW ANALYZE STATUS
```

如果输出结果为空,说明后台没有正在执行的 `ANALYZE` 任务
查看 `instance` 列和 `process_id` 列获得正在执行后台 `ANALYZE` 任务的 TiDB 实例地址和任务 `ID`

2. 使用客户端连接到执行后台 `ANALYZE` 任务的 TiDB 实例,然后执行以下 `KILL` 语句:
2. 终止正在后台运行的 `ANALYZE` 任务。

{{< copyable "sql" >}}

```sql
KILL TIDB ${id};
```

`${id}` 为上一步中查询得到的后台 `ANALYZE` 任务的 `ID`。

> **注意:**
>
> 只有当使用客户端连接到执行后台 `ANALYZE` 任务的 TiDB 实例时,执行 `KILL` 语句才能终止后台的 `ANALYZE` 任务。如果使用客户端连接到其他 TiDB 实例,或者客户端和 TiDB 中间有代理,`KILL` 语句不能终止后台的 `ANALYZE` 任务。更多信息,请参考 [`KILL [TIDB]`](/sql-statements/sql-statement-kill.md)。
- 如果 [`enable-global-kill`](/tidb-configuration-file.md#enable-global-kill-从-v610-版本开始引入) 的值为 `true` (默认为 `true`),你可以直接执行 `KILL TIDB ${id};` 语句。其中,${id} 为上一步中查询得到的后台 `ANALYZE` 任务的 `ID`。
- 如果 `enable-global-kill` 的值为 `false`,你需要先使用客户端连接到执行后台 `ANALYZE` 任务的 TiDB 实例,然后再执行 `KILL TIDB ${id};` 语句。如果使用客户端连接到其他 TiDB 实例,或者客户端和 TiDB 中间有代理,`KILL` 语句不能终止后台的 `ANALYZE` 任务。

关于 `KILL` 语句的更多信息,请参考 [`KILL [TIDB]`](/sql-statements/sql-statement-kill.md)。

### 控制 ANALYZE 并发度

Expand Down Expand Up @@ -415,17 +408,25 @@ SHOW ANALYZE STATUS [ShowLikeOrWhere];

该语句会输出 `ANALYZE` 的状态,可以通过使用 `ShowLikeOrWhere` 来筛选需要的信息。

目前 `SHOW ANALYZE STATUS` 会输出 7 列,具体如下:
目前 `SHOW ANALYZE STATUS` 会输出 11 列,具体如下:

| 列名 | 说明 |
| -------- | ------------- |
| table_schema | 数据库名 |
| table_name | 表名 |
| partition_name| 分区名 |
| job_info | 任务具体信息。如果分析索引则会包含索引名 |
| row_count | 已经分析的行数 |
| job_info | 任务具体信息。如果分析索引,该信息会包含索引名。当 `tidb_analyze_version =2` 时,该信息会包含采样率等配置项。 |
| processed_rows | 已经分析的行数 |
| start_time | 任务开始执行的时间 |
| end_time | 任务结束执行的时间 |
| state | 任务状态,包括 pending(等待)、running(正在执行)、finished(执行成功)和 failed(执行失败)|
| fail_reason | 任务失败的原因。如果执行成功则为 `NULL`。 |
| instance | 执行任务的 TiDB 实例 |
| process_id | 执行任务的 process ID |

从 TiDB v6.1.0 起,执行 `SHOW ANALYZE STATUS` 语句将显示集群级别的任务,且 TiDB 重启后仍能看到重启之前的任务记录。在 TiDB v6.1.0 之前,执行 `SHOW ANALYZE STATUS` 语句仅显示实例级别的任务,且 TiDB 重启后任务记录会被清空。

`SHOW ANALYZE STATUS` 仅显示最近的任务记录。从 TiDB v6.1 起,你可以通过系统表 `mysql.analyze_jobs` 查看过去 7 天内的历史记录。

当设置了 [`tidb_mem_quota_analyze`](/system-variables.md#tidb_mem_quota_analyze-从-v610-版本开始引入) 且 TiDB 后台的统计信息自动更新任务的内存占用超过了这个阈值时,自动更新任务会重试。失败的任务和重试的任务都可以在 `SHOW ANALYZE STATUS` 的结果中查看。

Expand Down