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 lumns 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
mysql group by order byWHERE (table_schema, table_name) NOT IN (
SELECT DISTINCT table_schema, table_name
FROM lumns
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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论