MySQL查看表占用空间大小

数据库 MySQL
在mysql中有一个默认的数据表information_schema,information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。

[[377200]]

 前言

在mysql中有一个默认的数据表information_schema,information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。

再简单点,这台MySQL服务器上,到底有哪些数据库、各个数据库有哪些表,每张表的字段类型是什么,各个数据库要什么权限才能访问,等等信息都保存在information_schema表里面,所以请勿删改此表。

代码

1,切换数据库

  1. use information_schema; 

2,查看数据库使用大小 

  1. select concat(round(sum(data_length/1024/1024),2),’MB’) as data from tables where table_schema=’DB_Name’ ; 

3,查看表使用大小 

  1. select concat(round(sum(data_length/1024/1024),2),’MB’) as data from tables where table_schema=’DB_Name’ and table_name=’Table_Name’; 

网上找的一个,亲测可用:

  •  先进去MySQL自带管理库:information_schema
  •  然后查询 data_length,index_length
  •  你自己的数据库名:dbname
  •  你自己的表名:tablename 
  1. mysql> use information_schema;    
  2. Database changed    
  3. mysql> select data_length,index_length    
  4.     -> from tables where    
  5.     -> table_schema='dbname'    
  6.     -> and table_name = 'tablename';   
  7. +-------------+--------------+    
  8. | data_length | index_length |    
  9. +-------------+--------------+    
  10. |   166379520 |    235782144 |    
  11. +-------------+--------------+    
  12. row in set (0.02 sec)    
  1. mysql> select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB,    
  2.     -> concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB    
  3.     -> from tables where    
  4.     -> table_schema='dbname'    
  5.     -> and table_name = 'tablename';   
  6. +----------------+-----------------+    
  7. | data_length_MB | index_length_MB |    
  8. +----------------+-----------------+    
  9. | 158.67MB       | 224.86MB        |    
  10. +----------------+-----------------+    
  11. row in set (0.03 sec) 

1.查看所有数据库容量大小 

  1. select  
  2. table_schema as '数据库',  
  3. sum(table_rows) as '记录数',  
  4. sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',  
  5. sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'  
  6. from information_schema.tables  
  7. group by table_schema  
  8. order by sum(data_length) desc, sum(index_length) desc;  
  9. ```   
  10. ### 2.查看所有数据库各表容量大小  
  11. ```sql  
  12. select  
  13. table_schema as '数据库',  
  14. table_name as '表名',  
  15. table_rows as '记录数',  
  16. truncate(data_length/1024/1024, 2) as '数据容量(MB)',  
  17. truncate(index_length/1024/1024, 2) as '索引容量(MB)'  
  18. from information_schema.tables  
  19. order by data_length desc, index_length desc; 

3.查看指定数据库容量大小

例:查看mysql库容量大小 

  1. select  
  2. table_schema as '数据库',  
  3. sum(table_rows) as '记录数',  
  4. sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',  
  5. sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'  
  6. from information_schema.tables  
  7. where table_schema='mysql'

4.查看指定数据库各表容量大小

例:查看mysql库各表容量大小 

  1. select  
  2. table_schema as '数据库',  
  3. table_name as '表名',  
  4. table_rows as '记录数',  
  5. truncate(data_length/1024/1024, 2) as '数据容量(MB)',  
  6. truncate(index_length/1024/1024, 2) as '索引容量(MB)'  
  7. from information_schema.tables  
  8. where table_schema='mysql'  
  9. order by data_length desc, index_length desc;  
  1. select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB, concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB  
  2. from tables  
  3. where table_schema='passport' and table_name='tb_user_info'

-- 569.98MB 141.98MB 

  1. select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB, concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB  
  2. from tables  
  3. where table_schema='passport_v2' and table_name='tb_user_info'

--   2128.94MB   285.00MB 

 

责任编辑:庞桂玉 来源: Java知音
相关推荐

2010-10-27 16:33:39

Oracle查看表空间

2011-04-13 09:31:50

Oracle

2010-04-16 10:00:06

Oracle查看表空间

2010-11-16 11:17:41

Oracle表空间大小

2010-07-28 10:13:06

DB2查询Table

2010-04-12 13:25:10

Oracle 数据库

2010-05-26 16:29:51

MySQL查看

2010-05-12 14:26:01

MySQL查看表结构

2010-04-07 11:39:16

Oracle常用

2011-07-18 15:59:17

MySQL数据库

2016-01-13 09:15:48

Java对象占空间

2010-08-18 09:18:10

DB2求剩余数据库空间

2012-05-02 14:22:55

端口占用

2015-09-30 14:38:19

系统磁盘空间Windows 10

2010-04-20 14:17:21

Unix操作系统

2022-04-01 10:37:45

戴尔

2009-11-24 17:20:48

Oracle查看用户表

2010-11-03 15:41:58

DB2重命名表

2010-11-03 11:26:39

DB2表空间

2024-01-24 16:31:34

数据中心虚拟化
点赞
收藏

51CTO技术栈公众号