Chinaunix首页 | 论坛 | 博客
  • 博客访问: 952569
  • 博文数量: 83
  • 博客积分: 32
  • 博客等级: 民兵
  • 技术积分: 2080
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-12 09:45
个人简介

努力学习,努力分享,努力.....在努力..努力离开..

文章分类

全部博文(83)

文章存档

2017年(3)

2015年(1)

2014年(47)

2013年(32)

分类: Mysql/postgreSQL

2014-05-15 14:53:37

在日常工作中经常会遇到领导让你查一下线上数据库占用多少空间啦.统计一下各个表的数据量啦.等等需求.
那么今天我们就以统计各表的数据量为题看看如何才能快速完成这种需求呢?

这里用到的函数是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.)

转载请注明出处.谢谢
阅读(1776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~