監控mysql索引使用效率的指令碼

babyyellow發表於2012-06-06
SELECT
  t.table_schema AS db,
  t.table_name   AS tab_name,
  s.index_name   AS index_name,
  s.column_name  AS field_name,
  s.seq_in_index  AS  seq_in_index,
  s2.max_columns AS max_col,
  s.cardinality  AS cardinality,
  t.table_rows   AS table_rows,
  ROUND(((s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) * 100), 2) AS sel
FROM information_schema.statistics s
  INNER JOIN information_schema.tables t
    ON s.table_schema = t.table_schema
      AND s.table_name = t.table_name
  INNER JOIN (SELECT
                table_schema,
                table_name,
                index_name,
                MAX(seq_in_index) AS max_columns
              FROM information_schema.statistics
              WHERE table_schema != 'mysql'
              GROUP BY table_schema, table_name, index_name) AS s2
    ON s.table_schema = s2.table_schema
      AND s.table_name = s2.table_name
      AND s.index_name = s2.index_name
WHERE t.table_schema != 'mysql'
    AND t.table_rows > 100
    AND s.CARDINALITY IS NOT NULL
    AND (s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) < 1.00
ORDER BY sel, s.table_schema, s.table_name
LIMIT 10

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-732013/,如需轉載,請註明出處,否則將追究法律責任。

相關文章