MySQL巡检SQL

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL巡检SQL相关的知识,希望对你有一定的参考价值。

#数据库对象
select db,name,lower(type) from mysql.proc where db not in ('sys','information_schema','performance_schema','mysql') union all SELECT table_schema,table_name,'view' from information_schema.views where table_schema not in ('sys','information_schema','performance_schema','mysql') union all SELECT TRIGGER_SCHEMA,TRIGGER_NAME,'trigger' FROM information_schema.triggers where TRIGGER_SCHEMA not in ('sys','information_schema','performance_schema','mysql');

#大表
SELECT table_schema,table_name,Round(Sum(data_length + index_length) / 1024 / 1024 / 1024, 1) data_size,round(data_free/1024/1024,1) as chip_size FROM information_schema.tables GROUP BY table_schema,table_name having data_size>20;

#自增id
SELECT a.table_schema,a.table_name, a.Auto_increment,b.COLUMN_TYPE  FROM information_schema.tables a  join information_schema.columns b on a.table_schema=b.table_schema and a.table_name=b.table_name where a.TABLE_SCHEMA not in ('information_schema','performance_schema', 'mysql', 'sys') and EXTRA='auto_increment' and b.COLUMN_TYPE like 'int%)' and a.Auto_increment>1500000000 union SELECT a.table_schema,a.table_name, a.Auto_increment,b.COLUMN_TYPE  FROM information_schema.tables a  join information_schema.columns b on a.table_schema=b.table_schema and a.table_name=b.table_name where a.TABLE_SCHEMA not in ('information_schema','performance_schema', 'mysql', 'sys') and EXTRA='auto_increment' and b.COLUMN_TYPE like 'int%unsigned'  and a.Auto_increment>3500000000;

#索引过多
select database_name,table_name,count(*) from mysql.innodb_index_stats where database_name='dbmetrics' and stat_description='Number of pages in the index' group by database_name,table_name having count(*) >6;

#索引字段过多
select database_name,table_name,index_name,count(*) from mysql.innodb_index_stats where database_name='dbmetrics' and stat_name not in ('size','n_leaf_pages') group by database_name,table_name,index_name having count(*) >3;

#无主键表
select table_schema, table_name from (select c.table_schema, c.table_name, case when column_key = 'PRI' then 1 else 0 end pk_flag   from (select table_schema, table_name      from information_schema.columns      where table_schema not in('information_schema', 'mysql', 'sys', 'performance_schema')     and ordinal_position = 1      and column_key != 'PRI') t,      information_schema.columns c      where t.table_schema = c.table_schema and t.table_name = c.table_name) i      group by table_schema, table_name      having sum(pk_flag) = 0;

以上是关于MySQL巡检SQL的主要内容,如果未能解决你的问题,请参考以下文章

TiDB数据库生成html格式的巡检报告

SQL SERVER日常运维巡检系列之七——日志

SQL SERVER日常运维巡检系列之三——结构设计

MS SQL巡检系列——检查外键字段是否缺少索引

SQL SERVER日常运维巡检系列之八——性能

美团 MySQL 数据库巡检系统的设计与应用