在日常工作中经常会遇到领导让你查一下线上数据库占用多少空间啦.统计一下各个表的数据量啦.等等需求.
那么今天我们就以统计各表的数据量为题看看如何才能快速完成这种需求呢?
这里用到的函数是CONCAT函数
举个例子简单看一下如何使用
mysql> select concat('a','b','c'),concat('d',null);
+---------------------+------------------+
| concat('a','b','c') | concat('d',null) |
+---------------------+------------------+
| abc | NULL |
+---------------------+------------------+
1 row in set (0.00 sec)
那么我们需要统计每个表的数据量用CONCAT函数形成批量脚本SQL语句如下:
mysql>select concat('select ','\'',table_name,'\',''\,\'',',count(*) from ',table_name,'; ') from information_schema.tables where table_schema='mysql';
结果如下:
+----------------------------------------------------------------------------------+
| concat('select ','\'',table_name,'\',''\,\'',',count(*) from ',table_name,'; ') |
+----------------------------------------------------------------------------------+
| select 'columns_priv',',',count(*) from columns_priv; |
| select 'db',',',count(*) from db; |
| select 'event',',',count(*) from event; |
| select 'func',',',count(*) from func; |
| select 'general_log',',',count(*) from general_log; |
| select 'help_category',',',count(*) from help_category; |
| select 'help_keyword',',',count(*) from help_keyword; |
.............
............
............
我们如何去掉讨厌的MySQL显示边框呢?
[root@localhost ]$ mysql -u root -pxxxxx -S/tmp/mysql.sock -N -s -e "select concat('select ','\'',table_name,'\',''\,\'',',count(*) from ',table_name,'; ') from information_schema.tables where table_schema='mysql';"
select 'columns_priv',',',count(*) from columns_priv;
select 'db',',',count(*) from db;
select 'event',',',count(*) from event;
select 'func',',',count(*) from func;
select 'general_log',',',count(*) from general_log;
select 'help_category',',',count(*) from help_category;
select 'help_keyword',',',count(*) from help_keyword;
select 'help_relation',',',count(*) from help_relation;
select 'help_topic',',',count(*) from help_topic;
select 'host',',',count(*) from host;
select 'ndb_binlog_index',',',count(*) from ndb_binlog_index;
select 'plugin',',',count(*) from plugin;
.................
.................
这样就可以了.然后直接保存成脚本source一下各表的数据就出来.保存成CSV格式用Excel排序一下就可以交差了.^_^
MySQL用的参数
-N, --skip-column-names Don't write column names in results.
-s, --silent Be more silent. Print results with a tab as separator, each row on new line.
-e, --execute=name Execute command and quit. (Disables --force and history file.)
转载请注明出处.谢谢
阅读(1827) | 评论(0) | 转发(0) |