docs/design/2020-09-30-index-usage-information.md
This document describes the design of recording index usage information.
There may be unused indexes in the database. In addition, modifying database tables, indexes, and query statements may cause some indexes to not be used in the future. Users may want to clear these long-unused indexes to relieve storage and performance pressure.
Related issues:
Design system tables to record index usage information. The system table is designed as follows:
mysql database and is named SCHEMA_INDEX_USAGE.SCHEMA_INDEX_USAGE:| Column name | Data type | Description |
|---|---|---|
| TABLE_ID | bigint | ID of the table or view on which the index is defined. |
| INDEX_ID | bigint | ID of the index. |
| QUERY_COUNT | longlong | Number of the SQL using this index. |
| ROWS_SELECTED | longlong | Number of rows read from the index. We can check the average fetched rows count of each query of the index through ROWS_READ / QUERY_COUNT. |
| LAST_USED_AT | timestamp | The last time of the SQL using this index. |
select * from mysql.SCHEMA_INDEX_USAGE.create table SCHEMA_INDEX_USAGE (
TABLE_ID bigint(21),
INDEX_ID bigint(21),
QUERY_COUNT BIGINT,
ROUWS_SELECTED BIGINT,
LAST_USED_AT timestamp,
PRIMARY KEY (SCHEMA_NAME, TABLE_NAME, INDEX_NAME)
);
According to the TiDB Doc, INDEX_ID should be bigint(21). Similarly, this doc tells us that TABLE_ID should be bigint(21).
insert into mysql.SCHEMA_INDEX_USAGE values (schemaA, tableA, indexA, count, row, used_at)
on duplicate key update query_count=query_count+count, rows_selected=rows_selected+row, last_used_at=greatest(last_used_at, used_at)
Statistics update is divided into statistics and persistence of index usage information. Index usage information is counted by the exec-info collector. And persistence is periodically writing data to system tables. We add a global variable index-usage-sync-lease to control the persistence cycle. It is set to 1 minute by default. In addition, add a global SQL variable to control whether to turn on this feature.
Due to MySQL compatibility, add the system table SCHEMA_UNUSED_INDEXES.
SCHEMA_UNUSED_INDEXES on table SCHEMA_INDEX_USAGE.| Column name | Data type | Description |
|---|---|---|
| object_schema | varchar | The schema name. |
| object_name | varchar | The table name. |
| index_name | varchar | The unused index name. |
create view information_schema.schema_unused_indexes
as select i.table_schema as table_schema, i.table_name as table_name, i.index_name as index_name
from mysql.tidb_indexes as i left join mysql.schema_index_usage as u
on i.table_schema=u.table_schema and i.table_name=u.table_name and i.index_name=u.index_name
where u.query_count=0 or u.query_count is null;
We use TABLE_ID and INDEX_ID as ID mysql.SCHEMA_INDEX_USAGE. Because of TABLE_ID and INDEX_ID is not user-friendly, we need a more user-friendly view.
Columns of it:
| Column name | Data type | Description |
|---|---|---|
| TABLE_SCHEMA | varchar | Name of the database on which the table or view is defined. |
| TABLE_NAME | varchar | Name of the table or view on which the index is defined. |
| INDEX_NAME | varchar | Name of the index. |
| QUERY_COUNT | longlong | Number of the SQL using this index. |
| ROWS_SELECTED | longlong | Number of rows read from the index. We can check the average fetched rows count of each query of the index through ROWS_READ / QUERY_COUNT. |
| LAST_USED_AT | timestamp | The last time of the SQL using this index. |
create view information_schema.schema_index_usage
as select idx.table_schema as table_schema, idx.table_name as table_name, idx.key_name as index_name, stats.query_count as query_count, stats.rows_selected as rows_selected
from mysql.schema_index_usage as stats, information_schema.tidb_indexes as idx, information_schema.tables as tables
where tables.table_name = idx.table_schema
AND tables.table_name = idx.table_name
AND tables.tidb_table_id = stats.table_id
AND idx.index_id = stats.index_id
Users may have just completed a deployment which changes query patterns such that they expect there will be unused indexes. They can potentially look at the LAST_USED_AT column, but sometimes flushing is more desired.
Similar usage: FLUSH INDEX_STATISTICS from https://www.percona.com/doc/percona-server/LATEST/diagnostics/user_stats.html.
SQL Syntax: FLUSH SCHEMA_INDEX_USAGE
Users can use this to initialize SCHEMA_INDEX_USAGE as
delete from mysql.schema_index_usage;
And it needs a RELOAD privilege check.
Read privilege: Reading SCHEMA_INDEX_USAGE and SCHEMA_UNUSED_INDEXES need permission. A user can see only the rows in the tables that correspond to tables for which the user has the proper access privileges, such as information_schema.tables. Write privilege: These tables are read-only. You can use FLUSH SCHEMA_INDEX_USAGE command to reinitialize tables, which requires the RELOAD privilege.
select * from schema_unused_indexes;select * from sys.dm_db_index_usage_stats;select * from v$object_usage;MySQL supports SCHEMA_UNUSED_INDEXES. We are considering compatibility and also support this view.
My implementation plan is in issues/19209
The test method is similar to general statistics.