当前位置:主页 > 查看内容

MySQL查看表占用空间大小

发布时间:2021-04-15 00:00| 位朋友查看

简介:前言 在mysql中有一个默认的数据表information_schema,information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。 再简单点,这台MySQL服务器上,到底有哪些数据库、各个数据库有哪些表,每张……

 前言

在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


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/m44Mz656pr7eqo7jhkiSaQ
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐