【推荐】单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。
说明:如果预计三年后的数据量根本达不到这个级别,请不要在创建表时就分库分表。
1. 查询所有数据库记录数和容量
SELECTtable_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. 查询指定数据库记录数和容量
SELECTtable_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、查询所有数据库每个表的记录数和容量
SELECTtable_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. 查询指定数据库每个表的记录数和容量
SELECTtable_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;