Skip to main content
Documents
Clauses StatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL Identifying Unused Indexes Indexes

Indexes in MySQL are database structures that improve the speed of data retrieval operations on a table. They are essential for optimizing query performance but can consume additional disk space and slow down write operations if unused.

Usage

Indexes are employed to quickly locate data without having to search every row in a database table. Identifying unused indexes helps to optimize database performance by removing unnecessary indexes that consume resources.

SHOW INDEX FROM table_name;

This syntax lists all indexes in the specified table, allowing you to analyze index usage and identify potential candidates for removal.

Examples

1. Listing Indexes for a Table

SHOW INDEX FROM employees;

This example retrieves all indexes from the employees table, providing details like index name, uniqueness, and the columns involved.

2. Using Performance Schema to Identify Unused Indexes

SELECT object_schema, object_name, index_name
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL AND count_star = 0;

This query uses the performance_schema to find indexes that have not been used during query executions, making them candidates for removal. Here, count_star represents the number of times an index has been accessed; a value of 0 indicates no access.

3. Analysis with Query Profiling

SET profiling = 1;
-- Run a set of queries
SELECT * FROM employees WHERE employee_id = 1;
SHOW PROFILE FOR QUERY 1;

By enabling profiling and analyzing query performance, you can determine which indexes are being utilized and which are not, aiding in the identification of unused indexes. Note that SHOW PROFILE FOR QUERY N; requires running specific queries beforehand. Remember to disable profiling after your analysis to avoid performance overhead:

SET profiling = 0;

Tips and Best Practices

  • Regularly monitor index usage. Use performance_schema or query profiling to consistently check index utilization. Ensure performance_schema is enabled and supported by your MySQL version.
  • Remove unused indexes. This helps to save disk space and improve performance for write operations. Use DROP INDEX to remove an index, but proceed with caution and consider testing in a non-production environment first.
  • Evaluate index necessity. Before creating new indexes, analyze query patterns to ensure they are necessary.
  • Balance index creation. Too many indexes can degrade performance during insert, update, and delete operations.

SQL Upskilling for Beginners

Gain the SQL skills to interact with and query your data.
Start Learning for Free