-
Notifications
You must be signed in to change notification settings - Fork 702
Add documents for information_schema.tidb_index_usage
and sys.schema_unused_index
.
#16446
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
7 commits
Select commit
Hold shift + click to select a range
3ccfa93
add documents for index usage
YangKeao 2149482
sync with the update in CN document
YangKeao fa97779
add notes about how to turn off this function
YangKeao 91af924
address comments
YangKeao 678f819
update tidb_enable_collect_execution_info
Oreoxmt 4a3c843
make ci happy
Oreoxmt d9b146f
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
103 changes: 103 additions & 0 deletions
103
information-schema/information-schema-tidb-index-usage.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
title: TIDB_INDEX_USAGE | ||
summary: Learn the `TIDB_INDEX_USAGE` INFORMATION_SCHEMA table. | ||
--- | ||
|
||
# TIDB_INDEX_USAGE | ||
|
||
<CustomContent platform="tidb"> | ||
|
||
Starting from v8.0.0, TiDB provides the `TIDB_INDEX_USAGE` table. You can use `TIDB_INDEX_USAGE` to get the usage statistics of all indexes on the current TiDB node. By default, TiDB collects these index usage statistics during SQL statement execution. You can disable this feature by turning off the [`instance.tidb_enable_collect_execution_info`](/tidb-configuration-file.md#tidb_enable_collect_execution_info) configuration item or the [`tidb_enable_collect_execution_info`](/system-variables.md#tidb_enable_collect_execution_info) system variable. | ||
|
||
</CustomContent> | ||
|
||
<CustomContent platform="tidb-cloud"> | ||
|
||
Starting from v8.0.0, TiDB provides the `TIDB_INDEX_USAGE` table. You can use `TIDB_INDEX_USAGE` to get the usage statistics of all indexes on the current TiDB node. By default, TiDB collects these index usage statistics during SQL statement execution. You can disable this feature by turning off the [`instance.tidb_enable_collect_execution_info`](https://docs.pingcap.com/tidb/v8.0/tidb-configuration-file#tidb_enable_collect_execution_info) configuration item or the [`tidb_enable_collect_execution_info`](/system-variables.md#tidb_enable_collect_execution_info) system variable. | ||
|
||
</CustomContent> | ||
|
||
```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) | ||
``` | ||
|
||
The columns in the `TIDB_INDEX_USAGE` table are as follows: | ||
|
||
* `TABLE_SCHEMA`: The name of the database to which the table containing the index belongs. | ||
* `TABLE_NAME`: The name of the table containing the index. | ||
* `INDEX_NAME`: The name of the index. | ||
* `QUERY_TOTAL`: The total number of statements accessing the index. | ||
* `KV_REQ_TOTAL`: The total number of KV requests generated when accessing the index. | ||
* `ROWS_ACCESS_TOTAL`: The total number of rows scanned when accessing the index. | ||
* `PERCENTAGE_ACCESS_0`: The number of times the row access ratio (the percentage of accessed rows out of the total number of rows in the table) is 0. | ||
* `PERCENTAGE_ACCESS_0_1`: The number of times the row access ratio is between 0% and 1%. | ||
* `PERCENTAGE_ACCESS_1_10`: The number of times the row access ratio is between 1% and 10%. | ||
* `PERCENTAGE_ACCESS_10_20`: The number of times the row access ratio is between 10% and 20%. | ||
* `PERCENTAGE_ACCESS_20_50`: The number of times the row access ratio is between 20% and 50%. | ||
* `PERCENTAGE_ACCESS_50_100`: The number of times the row access ratio is between 50% and 100%. | ||
* `PERCENTAGE_ACCESS_100`: The number of times the row access ratio is 100%. | ||
* `LAST_ACCESS_TIME`: The time of the most recent access to the index. | ||
|
||
## CLUSTER_TIDB_INDEX_USAGE | ||
|
||
The `TIDB_INDEX_USAGE` table only provides usage statistics of all indexes on a single TiDB node. To obtain the index usage statistics on all TiDB nodes in the cluster, you need to query the `CLUSTER_TIDB_INDEX_USAGE` table. | ||
|
||
Compared with the `TIDB_INDEX_USAGE` table, the query result of the `CLUSTER_TIDB_INDEX_USAGE` table includes an additional `INSTANCE` field. This field displays the IP address and port of each node in the cluster, which helps you distinguish the statistics across different nodes. | ||
|
||
```sql | ||
USE INFORMATION_SCHEMA; | ||
DESC CLUSTER_TIDB_INDEX_USAGE; | ||
``` | ||
|
||
The output is as follows: | ||
|
||
```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) | ||
``` | ||
|
||
## Limitations | ||
|
||
- The data in the `TIDB_INDEX_USAGE` table might be delayed by up to 5 minutes. | ||
- After TiDB restarts, the data in the `TIDB_INDEX_USAGE` table is cleared. |
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: Learn about the system tables in the `sys` schema. | ||
--- | ||
|
||
# `sys` Schema | ||
|
||
Starting from v8.0.0, TiDB provides the `sys` schema. You can use the views in `sys` schema to understand the data in the system tables, [`INFORMATION_SCHEMA`](/information-schema/information-schema.md), and [`PERFORMANCE SCHEMA`](/performance-schema/performance-schema.md) of TiDB. | ||
|
||
## Manually create the `sys` schema and views | ||
|
||
For clusters upgraded from versions earlier than v8.0.0, the `sys` schema and the views in it are not created automatically. You can manually create them using the following SQL statements: | ||
|
||
```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` records indexes that have not been used since the last start of TiDB. It includes the following columns: | ||
|
||
- `OBJECT_SCHEMA`: The name of the database to which the table containing the index belongs. | ||
- `OBJECT_NAME`: The name of the table containing the index. | ||
- `INDEX_NAME`: The name of the index. | ||
|
||
```sql | ||
USE SYS; | ||
DESC SCHEMA_UNUSED_INDEXES; | ||
``` | ||
|
||
The output is as follows: | ||
|
||
```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.