SELECT CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),'MB') AS data_size,
CONCAT(TRUNCATE(SUM(max_data_length)/1024/1024,2),'MB') AS max_data_size,
CONCAT(TRUNCATE(SUM(data_free)/1024/1024,2),'MB') AS data_free,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),'MB') AS index_size
FROM information_schema.tables WHERE TABLE_SCHEMA = 'db';

 

MySQL中有一个名为 information_schema 的数据库,在该库中有一个 TABLES 表,这个表主要字段分别是:

TABLE_SCHEMA : 数据库名
TABLE_NAME:表名
ENGINE:所使用的存储引擎
TABLES_ROWS:记录数
DATA_LENGTH:数据大小
INDEX_LENGTH:索引大小


use information_schema;  
SELECT   
   TABLE_NAME,  
    (DATA_LENGTH/1024/1024) as DataM ,  
    (INDEX_LENGTH/1024/1024) as IndexM,   
    ((DATA_LENGTH+INDEX_LENGTH)/1024/1024) as AllM,  
    TABLE_ROWS  
FROM  
    TABLES  
WHERE  
    TABLE_SCHEMA = 'fj_db';