felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

查看 MySQL 库/表/索引大小、连接数 #174

Open felix-cao opened 5 years ago

felix-cao commented 5 years ago

通过 MySQLinformation_schema 数据库,可查询数据库中每个表占用的空间、表记录的行数;该库中有一个 TABLES 表,这个表主要字段分别是:

其他字段请参考 MySQL的手册,查看一个表占用空间的大小,那就相当于是 数据大小 + 索引大小 。

查看所有库的大小

mysql> use information_schema;
Database changed
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES;
+----------+
| data     |
+----------+
| 868.34MB |
+----------+
1 row in set (0.04 sec)

查看指定库的大小

mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES where table_schema='fncftc_sql';
+----------+
| data     |
+----------+
| 858.37MB |
+----------+
1 row in set (0.01 sec)

查看指定库的指定表的大小

select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES where table_schema='fncftc_sql' and table_name='fanwe_money_detail';
+----------+
| data     |
+----------+
| 170.92MB |
+----------+
1 row in set (0.00 sec)

查看指定库的索引大小

mysql> select concat(round(sum(index_length/1024/1024),2),'MB') as 'Total Index Size'  from TABLES where table_schema='fncftc_sql';
+------------------+
| Total Index Size |
+------------------+
| 787.33MB         |
+------------------+
1 row in set (0.00 sec)

查看指定库的指定表的索引大小

mysql> select concat(round(sum(index_length/1024/1024),2),'MB') as 'Total Index Size'  from TABLES where table_schema='fncftc_sql' and table_name='fanwe_money_detail';
+------------------+
| Total Index Size |
+------------------+
| 256.72MB         |
+------------------+
1 row in set (0.00 sec)

查看一个库中的情况

mysql> 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 'fncftc_sql';
+------------------------------------------------------+----------------+-----------+------------+---------+
| Table Name                                           | Number of Rows | Data Size | Index Size | Total   |
+------------------------------------------------------+----------------+-----------+------------+---------+
| fncftc_sql.fanwe_about_rules                         | 0.0000M        | 0.0000G   | 0.0000G    | 0.0000G |
| fncftc_sql.fanwe_account_record                      | 0.0000M        | 0.0000G   | 0.0000G    | 0.0000G |
| fncftc_sql.fanwe_activit                             | 0.0000M        | 0.0000G   | 0.0000G    | 0.0000G |
| fncftc_sql.fanwe_activit_cash                        | 0.0000M        | 0.0000G   | 0.0000G    | 0.0000G |
| fncftc_sql.fanwe_activit_order                       | 0.0021M        | 0.0003G   | 0.0001G    | 0.0004G |

连接数

实时连接数

cd /usr/local/mysql/bin
./mysqladmin -uquma -p  processlist

mysql> show full processlist 

image

只查看当前连接数

./mysqladmin -uquma -p  status
Enter password: 
Uptime: 656028  Threads: 37  Questions: 117890362  Slow queries: 331  Opens: 10325  Flush tables: 15  Open tables: 659  Queries per second avg: 179.703

上面的Threads 就是连接数, 其他参数含义如下: