The Worst Performing Indexes


/ Published in: MySQL
Save to your folder(s)

This script shows the top 10 worst indexes (in terms of selectivity %) on the whole MySQL server instance. Selectivity is the percentage of distinct values in an indexed field compared to the number of records in the table.
Note that this query can take some time to complete on servers with lots of databases or lots of tables.

Jay Pipes (http://jpipes.com/)


Copy this code and paste it in your HTML
  1. /*
  2. SQL script to grab the worst performing indexes
  3. in the whole server
  4. */
  5. t.TABLE_SCHEMA AS `db`
  6. , t.TABLE_NAME AS `table`
  7. , s.INDEX_NAME AS `inde name`
  8. , s.COLUMN_NAME AS `field name`
  9. , s.SEQ_IN_INDEX `seq in index`
  10. , s2.max_columns AS `# cols`
  11. , s.CARDINALITY AS `card`
  12. , t.TABLE_ROWS AS `est rows`
  13. , ROUND(((s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) * 100), 2) AS `sel %`
  14. FROM INFORMATION_SCHEMA.STATISTICS s
  15. INNER JOIN INFORMATION_SCHEMA.TABLES t
  16. ON s.TABLE_SCHEMA = t.TABLE_SCHEMA
  17. AND s.TABLE_NAME = t.TABLE_NAME
  18. TABLE_SCHEMA
  19. , TABLE_NAME
  20. , INDEX_NAME
  21. , MAX(SEQ_IN_INDEX) AS max_columns
  22. FROM INFORMATION_SCHEMA.STATISTICS
  23. WHERE TABLE_SCHEMA != 'mysql'
  24. GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
  25. ) AS s2
  26. ON s.TABLE_SCHEMA = s2.TABLE_SCHEMA
  27. AND s.TABLE_NAME = s2.TABLE_NAME
  28. AND s.INDEX_NAME = s2.INDEX_NAME
  29. WHERE t.TABLE_SCHEMA != 'mysql' /* Filter out the mysql system DB */
  30. AND t.TABLE_ROWS > 10 /* Only tables with some rows */
  31. AND s.CARDINALITY IS NOT NULL /* Need at least one non-NULL value in the field */
  32. AND (s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) < 1.00 /* Selectivity < 1.0 b/c unique indexes are perfect anyway */
  33. ORDER BY `sel %`, s.TABLE_SCHEMA, s.TABLE_NAME /* Switch to `sel %` DESC for best non-unique indexes */
  34. LIMIT 10;

URL: http://forge.mysql.com/tools/tool.php?id=85

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.