Skip to content

Commit

Permalink
Update and correct information aboud sys.schema_unused_indexes (#16885)…
Browse files Browse the repository at this point in the history
… (#16902)
  • Loading branch information
ti-chi-bot authored Mar 29, 2024
1 parent e11833e commit 81d9e69
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 39 deletions.
3 changes: 3 additions & 0 deletions TOC-tidb-cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@
- PERFORMANCE_SCHEMA
- [Overview](/performance-schema/performance-schema.md)
- [`SESSION_CONNECT_ATTRS`](/performance-schema/performance-schema-session-connect-attrs.md)
- SYS
- [Overview](/sys-schema/sys-schema.md)
- [`schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md)
- [Metadata Lock](/metadata-lock.md)
- [Use UUIDs](/best-practices/uuid.md)
- [TiDB Accelerated Table Creation](/accelerated-table-creation.md)
Expand Down
4 changes: 3 additions & 1 deletion TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,9 @@
- PERFORMANCE_SCHEMA
- [Overview](/performance-schema/performance-schema.md)
- [`SESSION_CONNECT_ATTRS`](/performance-schema/performance-schema-session-connect-attrs.md)
- [`SYS`](/sys-schema.md)
- SYS
- [Overview](/sys-schema/sys-schema.md)
- [`schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md)
- [Metadata Lock](/metadata-lock.md)
- [TiDB Accelerated Table Creation](/accelerated-table-creation.md)
- UI
Expand Down
4 changes: 4 additions & 0 deletions information-schema/information-schema-tidb-index-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ The output is as follows:

- 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.

## Read more

- [`sys.schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md)
8 changes: 4 additions & 4 deletions information-schema/information-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Many `INFORMATION_SCHEMA` tables have a corresponding `SHOW` command. The benefi

<CustomContent platform="tidb">

| Table Name | Description |
| Table name | Description |
|-----------------------------------------------------------------------------------------|-----------------------------|
| [`CHARACTER_SETS`](/information-schema/information-schema-character-sets.md) | Provides a list of character sets the server supports. |
| [`CHECK_CONSTRAINTS`](/information-schema/information-schema-check-constraints.md) | Provides information about [`CHECK` constraints](/constraints.md#check) on tables. |
Expand Down Expand Up @@ -56,7 +56,7 @@ Many `INFORMATION_SCHEMA` tables have a corresponding `SHOW` command. The benefi

<CustomContent platform="tidb-cloud">

| Table Name | Description |
| Table name | Description |
|-----------------------------------------------------------------------------------------|-----------------------------|
| [`CHARACTER_SETS`](/information-schema/information-schema-character-sets.md) | Provides a list of character sets the server supports. |
| [`CHECK_CONSTRAINTS`](/information-schema/information-schema-check-constraints.md) | Provides information about [`CHECK` constraints](/constraints.md#check) on tables. |
Expand Down Expand Up @@ -104,7 +104,7 @@ Many `INFORMATION_SCHEMA` tables have a corresponding `SHOW` command. The benefi
>
> Some of the following tables are only supported on TiDB Self-Hosted and not supported on TiDB Cloud. To get a full list of unsupported tables on TiDB Cloud, see [System tables](https://docs.pingcap.com/tidbcloud/limited-sql-features#system-tables).
| Table Name | Description |
| Table name | Description |
|-----------------------------------------------------------------------------------------|-------------|
| [`ANALYZE_STATUS`](/information-schema/information-schema-analyze-status.md) | Provides information about tasks to collect statistics. |
| [`CLIENT_ERRORS_SUMMARY_BY_HOST`](/information-schema/client-errors-summary-by-host.md) | Provides a summary of errors and warnings generated by client requests and returned to clients. |
Expand Down Expand Up @@ -157,7 +157,7 @@ Many `INFORMATION_SCHEMA` tables have a corresponding `SHOW` command. The benefi

<CustomContent platform="tidb-cloud">

| Table Name | Description |
| Table name | Description |
|-----------------------------------------------------------------------------------------|-------------|
| [`ANALYZE_STATUS`](/information-schema/information-schema-analyze-status.md) | Provides information about tasks to collect statistics. |
| [`CLIENT_ERRORS_SUMMARY_BY_HOST`](/information-schema/client-errors-summary-by-host.md) | Provides a summary of errors and warnings generated by client requests and returned to clients. |
Expand Down
4 changes: 2 additions & 2 deletions performance-schema/performance-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TiDB implements performance schema tables for MySQL compatibility.

## Tables for MySQL compatibility

| Table Name | Description |
| Table name | Description |
|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| `events_stages_current` | |
| `events_stages_history` | |
Expand All @@ -33,7 +33,7 @@ TiDB implements performance schema tables for MySQL compatibility.

## Tables that are TiDB extensions

| Table Name | Description |
| Table name | Description |
|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| `pd_profile_allocs` | |
| `pd_profile_block` | |
Expand Down
6 changes: 3 additions & 3 deletions releases/release-8.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Quick access: [Quick start](https://docs.pingcap.com/tidb/v8.0/quick-start-with-
<tr>
<td rowspan="1">DB Operations and Observability</td>
<td>Support monitoring index usage statistics </td>
<td>Proper index design is a crucial prerequisite to maintaining database performance. TiDB v8.0.0 introduces the <a href="https://docs.pingcap.com/tidb/v8.0/information-schema-tidb-index-usage"><code>INFORMATION_SCHEMA.TIDB_INDEX_USAGE</code></a> table and the <a href="https://docs.pingcap.com/tidb/v8.0/sys-schema"><code>sys.schema_unused_index</code></a> view to provide usage statistics of indexes. This feature helps you assess the efficiency of indexes in the database and optimize the index design.</td>
<td>Proper index design is a crucial prerequisite to maintaining database performance. TiDB v8.0.0 introduces the <a href="https://docs.pingcap.com/tidb/v8.0/information-schema-tidb-index-usage"><code>INFORMATION_SCHEMA.TIDB_INDEX_USAGE</code></a> table and the <a href="https://docs.pingcap.com/tidb/v8.0/sys-schema-unused-indexes"><code>sys.schema_unused_indexes</code></a> view to provide usage statistics of indexes. This feature helps you assess the efficiency of indexes in the database and optimize the index design.</td>
</tr>
<tr>
<td rowspan="2">Data Migration</td>
Expand Down Expand Up @@ -236,7 +236,7 @@ Quick access: [Quick start](https://docs.pingcap.com/tidb/v8.0/quick-start-with-

With this information, you can identify indexes that are not used by the optimizer and indexes with poor selectivity, thereby optimizing index design to improve database performance.

Additionally, TiDB v8.0.0 introduces a view [`sys.schema_unused_index`](/sys-schema.md), which is compatible with MySQL. This view shows indexes that have not been used since the last start of TiDB instances. For clusters upgraded from versions earlier than v8.0.0, the `sys` schema and the views are not created automatically. You can manually create them by referring to [`sys`](/sys-schema.md).
Additionally, TiDB v8.0.0 introduces a view [`sys.schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md), which is compatible with MySQL. This view shows indexes that have not been used since the last start of TiDB instances. For clusters upgraded from versions earlier than v8.0.0, the `sys` schema and the views are not created automatically. You can manually create them by referring to [`sys.schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md#manually-create-the-schema_unused_indexes-view).

For more information, see [documentation](/information-schema/information-schema-tidb-index-usage.md).

Expand Down Expand Up @@ -362,7 +362,7 @@ Quick access: [Quick start](https://docs.pingcap.com/tidb/v8.0/quick-start-with-
### System tables

* Add new system tables [`INFORMATION_SCHEMA.TIDB_INDEX_USAGE`](/information-schema/information-schema-tidb-index-usage.md) and [`INFORMATION_SCHEMA.CLUSTER_TIDB_INDEX_USAGE`](/information-schema/information-schema-tidb-index-usage.md#cluster_tidb_index_usage) to record index usage statistics on TiDB nodes.
* Add a new system schema [`sys`](/sys-schema.md) and a new view [`sys.schema_unused_index`](/sys-schema.md#schema_unused_index), which records indexes that have not been used since the last start of TiDB.
* Add a new system schema [`sys`](/sys-schema/sys-schema.md) and a new view [`sys.schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md), which records indexes that have not been used since the last start of TiDB.

## Deprecated features

Expand Down
58 changes: 29 additions & 29 deletions sys-schema.md → sys-schema/sys-schema-unused-indexes.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
---
title: sys Schema
summary: Learn about the system tables in the `sys` schema.
title: schema_unused_indexes
summary: Learn about the `schema_unused_indexes` table in the `sys` schema.
---

# `sys` Schema
# `schema_unused_indexes`

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:
`schema_unused_indexes` 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.
Expand All @@ -51,4 +27,28 @@ The output is as follows:
| index_name | varchar(64) | YES | | NULL | |
+---------------+-------------+------+------+---------+-------+
3 rows in set (0.00 sec)
```
```

## Manually create the `schema_unused_indexes` view

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;
```

## Read more

- [`INFORMATION_SCHEMA.TIDB_INDEX_USAGE`](/information-schema/information-schema-tidb-index-usage.md)
14 changes: 14 additions & 0 deletions sys-schema/sys-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
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.

## Tables for MySQL compatibility

| Table name | Description |
|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| [`schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md) | Records indexes that have not been used since the last start of TiDB. |

0 comments on commit 81d9e69

Please sign in to comment.