schema_unused_indexes

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.
  • INDEX_NAME: The name of the index.
  1. USE SYS;
  2. DESC SCHEMA_UNUSED_INDEXES;

The output is as follows:

  1. +---------------+-------------+------+------+---------+-------+
  2. | Field | Type | Null | Key | Default | Extra |
  3. +---------------+-------------+------+------+---------+-------+
  4. | object_schema | varchar(64) | YES | | NULL | |
  5. | object_name | varchar(64) | YES | | NULL | |
  6. | index_name | varchar(64) | YES | | NULL | |
  7. +---------------+-------------+------+------+---------+-------+
  8. 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:

  1. CREATE DATABASE IF NOT EXISTS sys;
  2. CREATE OR REPLACE VIEW sys.schema_unused_indexes AS
  3. SELECT
  4. table_schema as object_schema,
  5. table_name as object_name,
  6. index_name
  7. FROM information_schema.cluster_tidb_index_usage
  8. WHERE
  9. table_schema not in ('sys', 'mysql', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA') and
  10. index_name != 'PRIMARY'
  11. GROUP BY table_schema, table_name, index_name
  12. HAVING
  13. sum(last_access_time) is null;

Read more