Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor unused indexes in MySQL

I have a set of servers where the maintenance query execution takes long. This is due to the indexes that are created by the developers at different points. Is there any way to monitor the unused indexes that adversely affect the execution.

like image 310
Luke Avatar asked Jan 25 '16 13:01

Luke


People also ask

How do you check if indexes are used or not?

In Oracle SQL Developer, when you have SQL in the worksheet, there is a button "Explain Plan", you can also hit F10. After you execute Explain plan, it will show in the bottom view of SQL Developer. There is a column "OBJECT_NAME", it will tell you what index is being used.

How do you check if indexes are being used in MySQL?

Your answer To see the index for a specific table use SHOW INDEX: SHOW INDEX FROM yourtable; To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA: SELECT DISTINCT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.

What happens if indexes are missing or not set correctly?

Indexes are one of the most important features of the SQL Server while being most overlooked and misused in practice. Their proper use leads to great server performance, but having them set up incorrectly or not having them set up at all, leads to poor execution times.

Which DMV should you use to find index utilization?

dm_db_index_usage_stats DMV result helps to decide if we need to remove that index or replace it with more optimal one.


1 Answers

For mysql 5.6 before

By Default package of mysql does not have much statistics for analysis but there are some unofficial patches by which you can analyze.

like userstats http://ourdelta.org/docs/userstats

By This patch you can analyze the stats and unused indexes.

Follow the below links to do that

https://www.percona.com/blog/2012/06/30/find-unused-indexes/

In MySQL 5.6 and later, the PERFORMANCE_SCHEMA tracks index IO. If an index has no IO activity, it has not been used.

The sys schema now provides a convenient view to make it easier to identify unused indexes:

http://www.markleith.co.uk/2011/04/18/monitoring-table-and-index-io-with-performance_schema/

DROP VIEW IF EXISTS unused_indexes;

CREATE VIEW unused_indexes AS
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
ORDER BY object_schema, object_name;

Note that this shows indexes that haven't been used since the last restart of mysqld. The PERFORMANCE_SCHEMA statistics are reset at startup time.

like image 190
developerCK Avatar answered Oct 07 '22 15:10

developerCK