Advertisement
krot

Ненужные индексы

Nov 14th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.15 KB | None | 0 0
  1. SELECT
  2. t.TABLE_SCHEMA AS `db`, t.TABLE_NAME AS `table`, s.INDEX_NAME AS `index name`
  3. , s.COLUMN_NAME AS `field name`, s.SEQ_IN_INDEX `seq in index`, s2.max_columns AS`# cols`
  4. , s.CARDINALITY AS `card`, t.TABLE_ROWS AS `est rows`
  5. , ROUND(((s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) * 100), 2) AS `sel %`
  6. FROM INFORMATION_SCHEMA.STATISTICS s
  7. INNER JOIN INFORMATION_SCHEMA.TABLES t
  8. ON s.TABLE_SCHEMA = t.TABLE_SCHEMA AND s.TABLE_NAME = t.TABLE_NAME
  9. INNER JOIN (
  10. SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME, MAX(SEQ_IN_INDEX) AS max_columns
  11. FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA != 'mysql'
  12. GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
  13. ) AS s2
  14. ON s.TABLE_SCHEMA = s2.TABLE_SCHEMA AND
  15. s.TABLE_NAME = s2.TABLE_NAME AND
  16. s.INDEX_NAME = s2.INDEX_NAME
  17. WHERE t.TABLE_SCHEMA != 'mysql'/* Filter out the mysql system DB */
  18. AND t.TABLE_ROWS > 10 /* Only tables with some rows */
  19. AND s.CARDINALITY IS NOT NULL/* Need at least one non-NULL value in the field */
  20. AND (s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) < 1.00 /* unique indexes are perfect anyway */
  21. ORDER BY `sel %`, s.TABLE_SCHEMA, s.TABLE_NAME /* DESC for best non-uniqueindexes */
  22. LIMIT 10;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement