mysql 案例~mysql元数据的sql统计
一 简介:今天我们来收集下提取元数据的sql
二 前沿: information_schema 引擎 memory 元数据收集表
三 sql语句:
1#没有使用索引的表统计
SELECT t.TABLE_SCHEMA,t.TABLE_NAME,t.TABLE_ROWS FROM information_schema.tables AS t LEFT JOIN (SELECT DISTINCT table_schema, table_name FROM information_schema.`KEY_COLUMN_USAGE` ) AS kt ON kt.table_schema=t.table_schema AND kt.table_name = t.table_name WHERE t.table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'test') AND kt.table_name IS NULL\G;
2#查看存储过程和函数
select routine_name,ROUTINE_SCHEMA,ROUTINE_TYPE from routines;
3 #查看使用myisam的表
select table_schema,table_name from information_schema.TABLES where ENGINE='MyISAM' and table_schema NOT IN ('information_schema','performance_schema','mysql')
4 #查看没有使用主键的表统计
SELECT t.table_schema,t.table_name,t.engine,IF(ISNULL(c.constraint_name),'NOPK','') AS nopk,IF(s.index_type = 'FULLTEXT','FULLTEXT','') as ftidx,IF(s.index_type = 'SPATIAL','SPATIAL','') as gisidx FROM information_schema.tables AS t LEFT JOIN information_schema.key_column_usage AS c ON (t.table_schema = c.constraint_schema AND t.table_name = c.table_name AND c.constraint_name = 'PRIMARY') LEFT JOIN information_schema.statistics AS s ON (t.table_schema = s.table_schema AND t.table_name = s.table_name AND s.index_type IN ('FULLTEXT','SPATIAL')) WHERE t.table_schema NOT IN ('information_schema','performance_schema','mysql') AND t.table_type = 'BASE TABLE' AND c.constraint_name IS NULL ORDER BY t.table_schema,t.table_name;
5 #行数据前10统计的表
SELECT table_schema,table_name,table_rows,data_length,index_length,CONCAT(ROUND((data_length+index_length)/(1024*1024),2),'M') AS 'Total',CONCAT(ROUND(DATA_FREE/(1024*1024),2),'M') FROM information_schema.TABLES where table_schema not in ('information_schema','mysql','performance_schema','test') order by table_rows desc limit 10;
6 #查看主外键约束
SELECT C.TABLE_SCHEMA,C.REFERENCED_TABLE_NAME,C.REFERENCED_COLUMN_NAME,C.TABLE_NAME,C.COLUMN_NAME,C.CONSTRAINT_NAME,T.TABLE_COMMENT,R.UPDATE_RULE,R.DELETE_RULE FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C JOIN INFORMATION_SCHEMA.TABLES T ON T.TABLE_NAME = C.TABLE_NAME JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R ON R.TABLE_NAME = C.TABLE_NAME AND R.CONSTRAINT_NAME = C.CONSTRAINT_NAME AND R.REFERENCED_TABLE_NAME = C.REFERENCED_TABLE_NAME WHERE C.REFERENCED_TABLE_NAME IS NOT NULL;
7 #查看触发器
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
8 #查看表整体(可以查看分区和具体约束)
SELECT %s as host,a.table_schema,a.table_name,c.CONSTRAINT_NAME,a.TABLE_TYPE,a.table_rows,c.CONSTRAINT_TYPE,CONCAT(ROUND((a.data_length+a.index_length)/(1024*1024),2),'M') AS 'Total',a.DATA_FREE,a.TABLE_COLLATION,b.PARTITION_NAME,b.PARTITION_EXPRESSION,b.PARTITION_METHOD,ENGINE FROM information_schema.TABLES a left join information_schema.PARTITIONS b on a.table_name=b.table_name left join information_schema.TABLE_CONSTRAINTS c on a.table_name=c.table_name where a.table_schema not in ('information_schema','mysql','performance_schema','test') group by a.table_name;
四 相关解释:
1 sql1语句收集没有任何索引的表(包括主键),这种表是非常危险的,应该避免出现
2 sql2语句收集存储过程和函数,有利于线上的规范
3 sql3 语句收集myisam表引擎的表,这种表会引起大量的表级锁,应该避免出现
4 sql4 语句收集没有主键的表,这种表会引起性能问题,应该避免出现
5 sql5 语句收集大表前10,大表对于mysql的性能影响很关键,应该尽量减少大表的出现
6 sql6 语句收集主外键约束的表,拥有主外键约束的表可以影响手动DML操作,DBA应该注意
7 sql7 语句收集触发器的表,用户触发器的表是无法应用pt工具的,DBA应该注意
8 收集整体表(可以查看分区和约束),关于数据库表的整体查看
五 设计
可以设计相关表收集这些数据进行展示,会进一步补充
六 这是我对于元数据收集的理解