查询数据库大小和表大小

表大小计算方法为data_length+index_length(包含碎片大小)

查询data和index大小

SELECT
	table_schema '库',
	TABLE_NAME AS '表',
	TRUNCATE(data_length / 1024 / 1024 / 1024,1 )as data_length,
	TRUNCATE(index_length / 1024 / 1024 / 1024 ,1) as index_length
FROM
	information_schema.TABLES 
GROUP BY
	TABLE_NAME
ORDER BY
	data_length desc

查询数据库总大小(data_length+index_length)

SELECT
	concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS data_length,
	concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS index_length 
FROM
	information_schema.TABLES;

查询所有库的大小(data_length+index_length)

SELECT
	table_schema AS '数据库',
	concat( TRUNCATE ( data_length / 1024 / 1024, 2 ), ' MB' ) AS '数据容量(MB)',
	concat( TRUNCATE ( index_length / 1024 / 1024, 2 ), ' MB' ) AS '索引容量(MB)' 
FROM
	information_schema.TABLES 
GROUP BY
	TABLE_SCHEMA 
ORDER BY
	data_length DESC;

查询某个库中所有表的大小(data_length+index_length)(准)(包含碎片大小)

SELECT
	TABLE_NAME AS '表名',
	concat( TRUNCATE ( data_length / 1024 / 1024, 0 ), ' MB' )+ concat( TRUNCATE ( index_length / 1024 / 1024, 0 ), ' MB' ) AS '表大小' 
FROM
	information_schema.TABLES 
WHERE
	TABLE_SCHEMA = 'enterpriseuser' 
GROUP BY
	TABLE_NAME 
ORDER BY
	表大小 DESC;

查询所有表的大小

SELECT
	table_schema '库',
	TABLE_NAME AS '表',
	concat( TRUNCATE ( data_length / 1024 / 1024 ,0)) + concat( TRUNCATE ( index_length / 1024 / 1024 ,0)) AS '表大小' 
FROM
	information_schema.TABLES 
GROUP BY
	TABLE_NAME 
ORDER BY
	表大小 DESC;

查询指定表的大小

SELECT
	table_schema '库',
	TABLE_NAME AS '表',
	concat(
		TRUNCATE ( data_length / 1024 / 1024, 0 )) + concat(
	TRUNCATE ( index_length / 1024 / 1024, 0 )) AS '表大小' 
FROM
	information_schema.TABLES 
WHERE
	table_schema = 'enterprise' 
	AND table_name = 'customeventdictionary';

GROUP BY
	TABLE_NAME 
ORDER BY
	表大小 DESC;


查询数据库大小和表大小
https://www.hechunyu.com/archives/cha-xun-shu-ju-ku-da-xiao-he-biao-da-xiao
作者
chunyu
发布于
2021年04月30日
许可协议