-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yang Keao <[email protected]>
- Loading branch information
Showing
3 changed files
with
100 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,35 +1,52 @@ | ||
--- | ||
title: sys Schema | ||
summary: 了解 sys 库中的系统表。 | ||
summary: 了解 TiDB `sys` 系统数据库。 | ||
--- | ||
|
||
# `sys` Schema | ||
|
||
> **注意:** | ||
> | ||
> 从 v8.0.0 及之前版本升级的集群将不包含 `sys` Schema 和其中的 View。它们可以通过以下 SQL 语句创建: | ||
> | ||
> {{< copyable "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; | ||
> ``` | ||
`sys` 库里的表或视图被用于帮助用户理解 TiDB 的系统表、`INFORMATION_SCHEMA` 表和 `Performance Schema` 表内的数据。 | ||
- `schema_unused_index` 用于记录自从 TiDB 上一次重启以来未使用的索引。 | ||
- `OBJECT_SCHEMA`:索引所在表的所属数据库的名称。 | ||
- `OBJECT_NAME`:索引所在表的名称。 | ||
- `INDEX_NAME`:索引的名称。 | ||
TiDB 从 v8.0.0 开始提供 `sys` Schema。你可以通过查看 `sys` 系统数据库中的表或视图理解 TiDB 的系统表、[`INFORMATION_SCHEMA` 表](/information-schema/information-schema.md)和 [`PERFORMANCE SCHEMA` 表](/performance-schema/performance-schema.md)内的数据。 | ||
|
||
对于从 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) | ||
``` |