MySQL--查询表统计信息
可以用show table status 来查看表的信息,如:
show table status like '%waybill5%' \G
但使用information_schema.`TABLES`更方便查看。
查看数据库级别的使用情况
SELECT TABLE_SCHEMA AS database_name, SUM(ROUND((DATA_LENGTH+INDEX_LENGTH+DATA_FREE)/1024.0/1024, 2)) AS Total_MB FROM information_schema.`TABLES` AS T1 WHERE T1.`TABLE_SCHEMA` NOT IN('performance_schema','mysql','information_schema') GROUP BY T1.`TABLE_SCHEMA` ORDER BY SUM(ROUND((DATA_LENGTH+INDEX_LENGTH+DATA_FREE)/1024.0/1024, 2)) DESC LIMIT 10;
查看非InnoDB引擎表
## 查看非InnoDB引擎表 SELECT TABLE_SCHEMA AS database_name, TABLE_NAME AS table_name, TABLE_ROWS AS table_rows, ENGINE AS table_engine, ROUND((DATA_LENGTH)/1024.0/1024, 2) AS Data_MB, ROUND((INDEX_LENGTH)/1024.0/1024, 2) AS Index_MB, ROUND((DATA_LENGTH+INDEX_LENGTH)/1024.0/1024, 2) AS Total_MB, ROUND((DATA_FREE)/1024.0/1024, 2) AS Free_MB FROM information_schema.`TABLES` AS T1 WHERE T1.`TABLE_SCHEMA` NOT IN('performance_schema','mysql','information_schema','sys') AND T1.`ENGINE` NOT IN ('innodb');
查看数据表较大的表
## 查看数据表量较大的表 SELECT TABLE_SCHEMA AS database_name, TABLE_NAME AS table_name, TABLE_ROWS AS table_rows, ENGINE AS table_engine, ROUND((DATA_LENGTH)/1024.0/1024, 2) AS Data_MB, ROUND((INDEX_LENGTH)/1024.0/1024, 2) AS Index_MB, ROUND((DATA_FREE)/1024.0/1024, 2) AS Free_MB, ROUND((DATA_LENGTH+INDEX_LENGTH+DATA_FREE)/1024.0/1024, 2) AS Total_MB FROM information_schema.`TABLES` AS T1 WHERE T1.`TABLE_SCHEMA` NOT IN('performance_schema','mysql','information_schema') ORDER BY T1.`TABLE_ROWS` DESC LIMIT 10; ## 查看存储空间较大的表 SELECT TABLE_SCHEMA AS database_name, TABLE_NAME AS table_name, TABLE_ROWS AS table_rows, ENGINE AS table_engine, ROUND((DATA_LENGTH)/1024.0/1024, 2) AS Data_MB, ROUND((INDEX_LENGTH)/1024.0/1024, 2) AS Index_MB, ROUND((DATA_FREE)/1024.0/1024, 2) AS Free_MB, ROUND((DATA_LENGTH+INDEX_LENGTH+DATA_FREE)/1024.0/1024, 2) AS Total_MB FROM information_schema.`TABLES` AS T1 WHERE T1.`TABLE_SCHEMA` NOT IN('performance_schema','mysql','information_schema') ORDER BY ROUND((DATA_LENGTH+INDEX_LENGTH+DATA_FREE)/1024.0/1024, 2) DESC LIMIT 10;
查看碎片较多的表
## 查看碎片较多的表 SELECT TABLE_SCHEMA AS database_name, TABLE_NAME AS table_name, TABLE_ROWS AS table_rows, ENGINE AS table_engine, ROUND((DATA_LENGTH)/1024.0/1024, 2) AS Data_MB, ROUND((INDEX_LENGTH)/1024.0/1024, 2) AS Index_MB, ROUND((DATA_FREE)/1024.0/1024, 2) AS Free_MB, ROUND((DATA_LENGTH+INDEX_LENGTH+DATA_FREE)/1024.0/1024, 2) AS Total_MB, ROUND(ROUND((DATA_FREE)/1024.0/1024, 2) /ROUND((DATA_LENGTH+INDEX_LENGTH)/1024.0/1024, 2)*100,2)AS Free_Percent FROM information_schema.`TABLES` AS T1 WHERE T1.`TABLE_SCHEMA` NOT IN('performance_schema','mysql','information_schema') AND ROUND(ROUND((DATA_FREE)/1024.0/1024, 2) /ROUND((DATA_LENGTH+INDEX_LENGTH)/1024.0/1024, 2)*100,2) >5 AND ROUND((DATA_FREE)/1024.0/1024, 2)>10 ORDER BY ROUND((DATA_FREE)/1024.0/1024, 2) DESC LIMIT 10;
查看表当前自增值
## 查看表自增值 SELECT T2.TABLE_SCHEMA, T2.TABLE_NAME, T1.COLUMN_NAME, T1.COLUMN_TYPE, T2.AUTO_INCREMENT FROM information_schema.columns AS T1 INNER JOIN information_schema.tables AS T2 ON T1.TABLE_SCHEMA=T2.TABLE_SCHEMA AND T1.TABLE_NAME=T2.TABLE_NAME WHERE T1.EXTRA='auto_increment' AND T1.DATA_TYPE NOT LIKE '%bigint%' ORDER BY T2.AUTO_INCREMENT DESC LIMIT 100;
查看无主键表
## 查看无主键表 SELECT TABLE_SCHEMA AS database_name, TABLE_NAME AS table_name, TABLE_ROWS AS table_rows, ENGINE AS table_engine, ROUND((DATA_LENGTH)/1024.0/1024, 2) AS Data_MB, ROUND((INDEX_LENGTH)/1024.0/1024, 2) AS Index_MB, ROUND((DATA_LENGTH+INDEX_LENGTH)/1024.0/1024, 2) AS Total_MB, ROUND((DATA_FREE)/1024.0/1024, 2) AS Free_MB FROM information_schema.tables WHERE (table_schema, table_name) NOT IN ( SELECT DISTINCT table_schema, table_name FROM information_schema.columns WHERE COLUMN_KEY = 'PRI' ) AND table_schema NOT IN ('sys', 'mysql', 'information_schema', 'performance_schema');
查看UTF8/UTF8MB4的表
## 查看使用utf8mb4字符集表 SELECT T1.TABLE_SCHEMA, T1.TABLE_NAME, T1.TABLE_TYPE, T1.TABLE_ROWS, T1.TABLE_COMMENT, T1.TABLE_COLLATION, CONCAT( 'ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' ) AS CHANGE_SQL FROM information_schema.TABLES T1 WHERE T1.TABLE_SCHEMA NOT IN('sys','performance_schema','information_schema','mysql') AND T1.TABLE_COLLATION LIKE 'utf8mb4%' ORDER BY T1.TABLE_ROWS DESC; ## 查看使用utf8字符集但应使用utf8mb4字符集的表 SELECT T1.TABLE_SCHEMA, T1.TABLE_NAME, T1.TABLE_TYPE, T1.TABLE_ROWS, T1.TABLE_COMMENT, T1.TABLE_COLLATION, CONCAT( 'ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' ) AS CHANGE_SQL FROM information_schema.TABLES T1 WHERE T1.TABLE_SCHEMA NOT IN('sys','performance_schema','information_schema','mysql') AND T1.TABLE_COLLATION LIKE 'utf8%' AND T1.TABLE_COLLATION NOT LIKE 'utf8mb4%' ORDER BY T1.TABLE_ROWS DESC; ## 查看使用utf8mb4字符集但未使用utf8mb4_general_ci排序规则的表 SELECT T1.TABLE_SCHEMA, T1.TABLE_NAME, T1.TABLE_TYPE, T1.TABLE_ROWS, T1.TABLE_COMMENT, T1.TABLE_COLLATION, CONCAT( 'ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' ) AS CHANGE_SQL FROM information_schema.TABLES T1 WHERE T1.TABLE_SCHEMA NOT IN('sys','performance_schema','information_schema','mysql') AND T1.TABLE_COLLATION<>'utf8mb4_general_ci' AND T1.TABLE_COLLATION LIKE 'utf8mb4_%' ORDER BY T1.TABLE_ROWS DESC;