MySQL数据库统计常用语句

Posted dbstack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL数据库统计常用语句相关的知识,希望对你有一定的参考价值。

1、查看所有数据库容量大小
select 
table_schema as \'数据库\',
sum(table_rows) as \'记录数\',
sum(truncate(data_length/1024/1024, 2)) as \'数据容量(MB)\',
sum(truncate(index_length/1024/1024, 2)) as \'索引容量(MB)\'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;
2、查看所有数据库各表容量大小
select 
table_schema as \'数据库\',
table_name as \'表名\',
table_rows as \'记录数\',
truncate(data_length/1024/1024, 2) as \'数据容量(MB)\',
truncate(index_length/1024/1024, 2) as \'索引容量(MB)\'
from information_schema.tables
order by data_length desc, index_length desc;
3、查看指定数据库各表容量大小
select 
table_schema as \'数据库\',
table_name as \'表名\',
table_rows as \'记录数\',
truncate(data_length/1024/1024, 2) as \'数据容量(MB)\',
truncate(index_length/1024/1024, 2) as \'索引容量(MB)\'
from information_schema.tables
where table_schema=\'zx_sms\'
order by data_length desc, index_length desc limit 10;

MySQL之常用SQL语句

  1、按小时统计的语句

    select
    concat(date_format(gmt_create, "%Y-%m-%d %k:00~"), hour(gmt_create)+1, ":00") as ‘time‘,
    count(*) as num
    from t_order
    where gmt_create>=‘2016-03-06 00:00:00‘ and gmt_create<=‘2016-03-06 23:59:59‘
    group by left(gmt_create, 13);

    select
    concat(date_format(gmt_create, "%Y-%m-%d %k:00~"), hour(gmt_create)+1, ":00") as ‘time‘,
    count(*) as num
    from t_order
    where gmt_create>=‘2016-03-06 00:00:00‘ and gmt_create<=‘2016-03-06 23:59:59‘
    group by date_format(a.gmt_create,‘%Y-%m-%d %H:00‘);

  2、加上序号
    select
    (@rowNO := @rowNo+1) as rowno,
    concat(date_format(a.gmt_create, "%Y-%m-%d %k:00~"), hour(a.gmt_create)+1, ":00") as ‘time‘,
    count(*) as num
    from t_order a,(select @rowNO :=0) b
    where a.gmt_create>=‘2016-03-06 00:00:00‘ and a.gmt_create<=‘2016-03-06 23:59:59‘
    group by date_format(a.gmt_create,‘%Y-%m-%d %H:00‘);

  3、环比,就是相邻时间段的对比。如:14年4月和14年3月是相邻时间段,这两个时间段的数据对比,就是环比。
    select date_format(a.m_adddate,‘%Y-%m‘) as 时间, count(*) as `当月`,
    (select count(*) from job_myreceive where date_format(a.m_adddate,‘%Y%m‘) = date_format(date_add(m_adddate,interval 1 month),‘%Y%m‘)) as 上月
    from job_myreceive a group by 1

  4、同比,是指在相邻时段中的某一相同时间点进行比较。
    如:13年和14年是相邻时段,13年3月和14年3月是这两个相邻时段的同一个时间点,都是3月,这两个时段进行数据对比,就是同比。

  5、查看数据库的大小,结果是以字节为单位,除1024为K,除1048576为M。
    select TABLE_SCHEMA ‘数据库名‘,(sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1048576 ‘大小(M)‘ from information_schema.tables where TABLE_SCHEMA=‘xiancheng‘;

  6、查看表的大小,结果是以字节为单位,除1024为K,除1048576为M。
    select TABLE_NAME ‘表名‘,(DATA_LENGTH+INDEX_LENGTH)/1048576 ‘大小(M)‘ from information_schema.tables where TABLE_SCHEMA=‘xiancheng‘;


























以上是关于MySQL数据库统计常用语句的主要内容,如果未能解决你的问题,请参考以下文章

MySQL总结

MySQL之常用SQL语句

Linux-MySQL-常用sql语句

mysql常用操作

搜集SQL常用的操作语句

常用SQL语句