-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add documents for information_schema.tidb_index_usage
and sys.schema_unused_index
.
#16511
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1dc5a34
add document for index usage
YangKeao 9172d7f
address comments
YangKeao ed2ec93
address comments
YangKeao 1e77327
add notes about how to turn off index usage collection
YangKeao 4aeff9a
address comment
YangKeao c784466
Apply suggestions from code review
Oreoxmt 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: TIDB_INDEX_USAGE | ||
summary: 了解 INFORMATION_SCHEMA 表 `TIDB_INDEX_USAGE`。 | ||
--- | ||
|
||
# TIDB_INDEX_USAGE | ||
|
||
TiDB 从 v8.0.0 开始提供 `TIDB_INDEX_USAGE` 表,你可以使用该表查看当前 TiDB 节点中所有索引的访问统计信息。在 SQL 语句执行时,TiDB 默认维护访问索引有关的统计信息,可以通过修改配置项 [`instance.tidb_enable_collect_execution_info`](/tidb-configuration-file.md#tidb_enable_collect_execution_info) 或者系统变量 [`tidb_enable_collect_execution_info`](/system-variables.md#tidb_enable_collect_execution_info) 将其关闭。 | ||
|
||
```sql | ||
USE INFORMATION_SCHEMA; | ||
DESC TIDB_INDEX_USAGE; | ||
``` | ||
|
||
```sql | ||
+--------------------------+-------------+------+------+---------+-------+ | ||
| Field | Type | Null | Key | Default | Extra | | ||
+--------------------------+-------------+------+------+---------+-------+ | ||
| TABLE_SCHEMA | varchar(64) | YES | | NULL | | | ||
| TABLE_NAME | varchar(64) | YES | | NULL | | | ||
| INDEX_NAME | varchar(64) | YES | | NULL | | | ||
| QUERY_TOTAL | bigint(21) | YES | | NULL | | | ||
| KV_REQ_TOTAL | bigint(21) | YES | | NULL | | | ||
| ROWS_ACCESS_TOTAL | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_0 | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_0_1 | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_1_10 | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_10_20 | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_20_50 | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_50_100 | bigint(21) | YES | | NULL | | | ||
| PERCENTAGE_ACCESS_100 | bigint(21) | YES | | NULL | | | ||
| LAST_ACCESS_TIME | datetime | YES | | NULL | | | ||
+--------------------------+-------------+------+------+---------+-------+ | ||
14 rows in set (0.00 sec) | ||
``` | ||
|
||
`TIDB_INDEX_USAGE` 表中列的含义如下: | ||
|
||
* `TABLE_SCHEMA`:索引所在表的所属数据库的名称。 | ||
* `TABLE_NAME`:索引所在表的名称。 | ||
* `INDEX_NAME`:索引的名称。 | ||
* `QUERY_TOTAL`:访问该索引的语句总数。 | ||
* `KV_REQ_TOTAL`:访问该索引时产生的 KV 请求总数。 | ||
* `ROWS_ACCESS_TOTAL`:访问该索引时扫描的总行数。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 对于大表,这个字段一直累加会不会有溢出的风险?需要特殊说明吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 假设集群每秒钟总共扫描 0xFFFFFFFF 行,uint64 在 0xFFFFFFFF s = 136.19252 年的时候才会溢出,现在看应该没啥风险 🤔 。 |
||
* `PERCENTAGE_ACCESS_0`:行访问比例(访问行数占表总行数的百分比)为 0 的次数。 | ||
* `PERCENTAGE_ACCESS_0_1`:行访问比例为 0 到 1% 的次数。 | ||
* `PERCENTAGE_ACCESS_1_10`:行访问比例为 1% 到 10% 的次数。 | ||
* `PERCENTAGE_ACCESS_10_20`:行访问比例为 10% 到 20% 的次数。 | ||
* `PERCENTAGE_ACCESS_20_50`:行访问比例为 20% 到 50% 的次数。 | ||
* `PERCENTAGE_ACCESS_50_100`:行访问比例为 50% 到 100% 的次数。 | ||
* `PERCENTAGE_ACCESS_100`:行访问比例为 100% 的次数。 | ||
* `LAST_ACCESS_TIME`:最近一次访问该索引的时间。 | ||
|
||
## CLUSTER_TIDB_INDEX_USAGE | ||
|
||
`TIDB_INDEX_USAGE` 表仅提供单个 TiDB 节点中所有索引的访问统计信息。如果要查看整个集群上所有 TiDB 节点中索引的访问统计信息,需要查询 `CLUSTER_TIDB_INDEX_USAGE` 表。 | ||
|
||
与 `TIDB_INDEX_USAGE` 表的查询结果相比,`CLUSTER_TIDB_INDEX_USAGE` 表的查询结果额外包含了 `INSTANCE` 字段。`INSTANCE` 字段展示了集群中各节点的 IP 地址和端口,用于区分不同节点上的统计信息。 | ||
|
||
```sql | ||
USE INFORMATION_SCHEMA; | ||
DESC CLUSTER_TIDB_INDEX_USAGE; | ||
``` | ||
|
||
输出结果如下: | ||
|
||
```sql | ||
+-------------------------+-----------------------------------------------------------------+------+------+---------+-------+ | ||
| Field | Type | Null | Key | Default | Extra | | ||
+-------------------------+-----------------------------------------------------------------+------+------+---------+-------+ | ||
| INSTANCE | varchar(64) | YES | | NULL | | | ||
| ID | bigint(21) unsigned | NO | PRI | NULL | | | ||
| START_TIME | timestamp(6) | YES | | NULL | | | ||
| CURRENT_SQL_DIGEST | varchar(64) | YES | | NULL | | | ||
| CURRENT_SQL_DIGEST_TEXT | text | YES | | NULL | | | ||
| STATE | enum('Idle','Running','LockWaiting','Committing','RollingBack') | YES | | NULL | | | ||
| WAITING_START_TIME | timestamp(6) | YES | | NULL | | | ||
| MEM_BUFFER_KEYS | bigint(64) | YES | | NULL | | | ||
| MEM_BUFFER_BYTES | bigint(64) | YES | | NULL | | | ||
| SESSION_ID | bigint(21) unsigned | YES | | NULL | | | ||
| USER | varchar(16) | YES | | NULL | | | ||
| DB | varchar(64) | YES | | NULL | | | ||
| ALL_SQL_DIGESTS | text | YES | | NULL | | | ||
| RELATED_TABLE_IDS | text | YES | | NULL | | | ||
| WAITING_TIME | double | YES | | NULL | | | ||
+-------------------------+-----------------------------------------------------------------+------+------+---------+-------+ | ||
15 rows in set (0.00 sec) | ||
``` | ||
|
||
## 使用限制 | ||
|
||
- `TIDB_INDEX_USAGE` 表中的数据可能存在最多 5 分钟的延迟。 | ||
- 在 TiDB 重启后,`TIDB_INDEX_USAGE` 表中的数据会被清空。 |
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,54 @@ | ||
--- | ||
title: sys Schema | ||
summary: 了解 TiDB `sys` 系统数据库。 | ||
--- | ||
|
||
# `sys` Schema | ||
|
||
TiDB 从 v8.0.0 开始提供 `sys` Schema。你可以通过查看 `sys` 系统数据库中的表或视图理解 TiDB 的系统表、[`INFORMATION_SCHEMA`](/information-schema/information-schema.md) 表和 [`PERFORMANCE SCHEMA`](/performance-schema/performance-schema.md) 表内的数据。 | ||
|
||
## 手动创建 `sys` Schema 和视图 | ||
|
||
Oreoxmt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
对于从 v8.0.0 之前版本升级的集群,`sys` Schema 和其中的视图不会自动创建。你可以通过以下 SQL 语句手动创建: | ||
|
||
```sql | ||
CREATE DATABASE IF NOT EXISTS sys; | ||
CREATE OR REPLACE VIEW sys.schema_unused_indexes AS | ||
SELECT | ||
table_schema as object_schema, | ||
table_name as object_name, | ||
index_name | ||
FROM information_schema.cluster_tidb_index_usage | ||
WHERE | ||
table_schema not in ('sys', 'mysql', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA') and | ||
index_name != 'PRIMARY' | ||
GROUP BY table_schema, table_name, index_name | ||
HAVING | ||
sum(last_access_time) is null; | ||
``` | ||
|
||
## `schema_unused_index` | ||
|
||
`schema_unused_index` 用于记录自 TiDB 上次启动以来未被使用的索引信息,包括如下列: | ||
|
||
- `OBJECT_SCHEMA`:索引所在表的所属数据库的名称。 | ||
- `OBJECT_NAME`:索引所在表的名称。 | ||
- `INDEX_NAME`:索引的名称。 | ||
|
||
```sql | ||
USE SYS; | ||
DESC SCHEMA_UNUSED_INDEXES; | ||
``` | ||
|
||
输出结果如下: | ||
|
||
```sql | ||
+---------------+-------------+------+------+---------+-------+ | ||
| Field | Type | Null | Key | Default | Extra | | ||
+---------------+-------------+------+------+---------+-------+ | ||
| object_schema | varchar(64) | YES | | NULL | | | ||
| object_name | varchar(64) | YES | | NULL | | | ||
| index_name | varchar(64) | YES | | NULL | | | ||
+---------------+-------------+------+------+---------+-------+ | ||
3 rows in set (0.00 sec) | ||
``` |
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.
Uh oh!
There was an error while loading. Please reload this page.