MYSQL - 算一算1000万条数据大概占了多大磁盘空间

假设有张表叫table_name;其中有四个字段

CREATE TABLE `table_name` (

id bigint(20) not null auto_increment,

detail varchar(2000),

createtime  datetime,

validity int default '0',

primary key (id)

);

每个字段设计完成后占用的字节数

bigint 8字节 

varchar 2000 字节 

datetime  8字节 

validity 4字节

算乘法计算占用磁盘:

(8+2000+8+4) * 10000000 = 20200000000 字节 == 18G

 

此时大概明白,为什么要在设计数据库的时候,采用最小类型的原则了吧

 

其实该计算公式仅为简单计算;mysql索引和表分离本身是分离,有时索引占用的磁盘空间可能比表占用的磁盘空间都要大!!

参看SQL:

1. 库空间以及索引空间大小:

select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
    concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
     from information_schema.tables
     group by TABLE_SCHEMA
     order by data_length desc;

2. 查询某个数据库内每张表的大小:

     concat(truncate(index_length/1024/1024,2),' MB') as index_size
     from information_schema.tables where TABLE_SCHEMA = 'metadata'
     group by TABLE_NAME
     order by data_length desc;

3.查看数据库中所有表的信息

SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', 
CONCAT(truncate(table_rows/1000000,2),'M') AS 'Number of Rows', 
CONCAT(truncate(data_length/(1024*1024*1024),2),'G') AS 'Data Size', 
CONCAT(truncate(index_length/(1024*1024*1024),2),'G') AS 'Index Size' , 
CONCAT(truncate((data_length+index_length)/(1024*1024*1024),2),'G') AS  'Total' FROM information_schema.TABLES WHERE table_schema LIKE 'metadata'; 

 

你可能感兴趣的:(数据库)