mysql查看数据库表容量大小

【推荐】单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。
说明:如果预计三年后的数据量根本达不到这个级别,请不要在创建表时就分库分表。

1. 查询所有数据库记录数和容量

SELECT
	table_schema AS '数据库',
	SUM(table_rows) AS '记录数',
	SUM(TRUNCATE(data_length/1024/1024, 2)) AS '数据容量(MB)',
	SUM(TRUNCATE(index_length/1024/1024, 2)) AS '索引容量(MB)'
FROM information_schema.tables
GROUP BY table_schema
ORDER BY SUM(data_length) DESC, SUM(index_length) DESC;

2. 查询指定数据库记录数和容量

SELECT
	table_schema AS '数据库',
	SUM( table_rows ) AS '记录数',
	SUM(TRUNCATE ( data_length / 1024 / 1024, 2 )) AS '数据容量(MB)',
	SUM(TRUNCATE ( index_length / 1024 / 1024, 2 )) AS '索引容量(MB)' 
FROM information_schema.TABLES 
WHERE table_schema = 'syspt';

3、查询所有数据库每个表的记录数和容量

SELECT
	table_schema AS '数据库',
	table_name AS '表名',
	table_rows AS '记录数',
	TRUNCATE(data_length/1024/1024, 2) AS '数据容量(MB)',
	TRUNCATE(index_length/1024/1024, 2) AS '索引容量(MB)'
FROM information_schema.tables
ORDER BY data_length DESC, index_length DESC;

4. 查询指定数据库每个表的记录数和容量

SELECT
	table_schema AS '数据库',
	table_name AS '表名',
	table_rows AS '记录数',
	TRUNCATE(data_length/1024/1024, 2) AS '数据容量(MB)',
	TRUNCATE(index_length/1024/1024, 2) AS '索引容量(MB)'
FROM information_schema.tables
WHERE table_schema='syspt'
ORDER BY data_length DESC, index_length DESC;

5. 统计所有数据库的总大小

SELECT CONCAT(ROUND(SUM(DATA_LENGTH/1024/1024),2),'MB') AS DATA FROM information_schema.TABLES;
相关推荐
知初~3 小时前
出行项目案例
hive·hadoop·redis·sql·mysql·spark·database
山猪打不过家猪4 小时前
ASP.NET Core Clean Architecture
java·数据库·asp.net
子非衣4 小时前
MySQL修改JSON格式数据示例
android·mysql·json
qwy7152292581634 小时前
13-R数据重塑
服务器·数据库·r语言
Bio Coder4 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言
钊兵5 小时前
数据库驱动免费下载(Oracle、Mysql、达梦、Postgresql)
数据库·mysql·postgresql·oracle·达梦·驱动
weixin_425878236 小时前
Redis复制性能优化利器:深入解析replica-lazy-flush参数
数据库·redis·性能优化
左灯右行的爱情7 小时前
Redis数据结构总结-listPack
数据结构·数据库·redis
隔壁老王1567 小时前
mysql实时同步到es
数据库·mysql·elasticsearch