查看mysql库大小,表大小,索引大小

查看mysql库大小,表大小,索引大小
通过MySQL的 information_schema 数据库,可查询数据库中每个表占用的空间、表记录的行数;该库中有一个 TABLES 表,这个表主要字段分别是:
TABLE_SCHEMA : 数据库名
TABLE_NAME:表名
ENGINE:所使用的存储引擎
TABLES_ROWS:记录数
DATA_LENGTH:数据大小
INDEX_LENGTH:索引大小
其他字段请参考MySQL的手册,查看一个表占用空间的大小,那就相当于是 数据大小 + 索引大小 。

 
1 查看所有库的大小
mysql> use information_schema;
Database changed
mysql> select concat(round(sum(DATA_LENGTH/1024/1024/1024),2),'G') as data  from TABLES;
+--------+
| data   |
+--------+
| 54.09G |
+--------+
1 row in set (0.01 sec)

2 查看指定库的大小
use information_schema;
select concat(round(sum(DATA_LENGTH/1024/1024/1024),2),'G') as data  from TABLES where table_schema='cps_message';
+---------+
| data    |
+---------+
| 26.17MB |
+---------+
1 row in set (0.01 sec)

3 查看指定库的指定表的大小
mysql> select concat(round(sum(DATA_LENGTH/1024/1024/1024),2),'G') as data  from TABLES where table_schema='gps_hisData' and table_name='gpsInfo';
+--------+
| data   |
+--------+
| 13.38G |
+--------+
1 row in set (0.00 sec)

4 查看指定库的索引大小
mysql> SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024*1024), 2), ' MB') AS 'Total Index Size' FROM TABLES  WHERE table_schema = 'cps_message'; 
+------------------+
| Total Index Size |
+------------------+
| 18.49 MB         |
+------------------+
1 row in set (0.00 sec)

5 查看指定库的指定表的索引大小
SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024*1024), 2), ' G') AS 'Total Index Size' FROM TABLES  WHERE table_schema = 'gps_hisData' and table_name='gpsInfo'; 
+------------------+
| Total Index Size |
+------------------+
| 21.84 MB         |
+------------------+
1 row in set (0.00 sec)

mysql> show create table gps_hisData.gpsInfo\G;
 
mysql> select count(*) from gps_hisData.gpsInfo;
+----------+
| count(*) |
+----------+
|  1073607 |
+----------+
1 row in set (0.00 sec)

6 查看一个库中的情况
SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', CONCAT(ROUND(table_rows/1000000,4),'M') AS 'Number of Rows', CONCAT(ROUND(data_length/(1024*1024*1024),4),'G') AS 'Data Size', CONCAT(ROUND(index_length/(1024*1024*1024),4),'G') AS 'Index Size', CONCAT(ROUND((data_length+index_length)/(1024*1024*1024),4),'G') AS'Total'FROM information_schema.TABLES WHERE table_schema LIKE 'gps_hisData';
+---------------------+----------------+-----------+------------+----------+
| Table Name          | Number of Rows | Data Size | Index Size | Total    |
+---------------------+----------------+-----------+------------+----------+
| gps_hisData.alarm   | 0.1447M        | 0.0226G   | 0.0363G    | 0.0588G  |
| gps_hisData.gpsInfo | 98.1350M       | 13.3805G  | 6.2616G    | 19.6421G |
+---------------------+----------------+-----------+------------+----------+
2 rows in set (0.01 sec)


参考网址:
http://www.oschina.net/question/12_3673
http://blog.sina.com.cn/s/blog_4c197d420101fbl9.html

猜你喜欢

转载自blog.csdn.net/w892824196/article/details/88183597