mysql查询数据库和每个表所占空间大小

首先查询所有数据库占用磁盘空间大小的SQL语句如下:

select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),’ MB’) as data_size,
concat(truncate(sum(index_length)/1024/1024,2),’MB’) as index_size
from information_schema.tables
group by TABLE_SCHEMA
order by data_length desc;

然后是查询单个数据库里面各个表所占磁盘空间大小包括其索引的大小,SQL语句如下:

select TABLE_NAME, concat(truncate(data_length/1024/1024,2),’ MB’) as data_size,
concat(truncate(index_length/1024/1024,2),’ MB’) as index_size
from information_schema.tables where TABLE_SCHEMA = ‘你数据库的名称’
group by TABLE_NAME
order by data_length desc;

mysql 查看数据库中所有表的记录数

use information_schema;
select table_name,table_rows from tables
where TABLE_SCHEMA = ‘you db name’
order by table_rows desc;

 

要想知道每个数据库的大小的话,步骤如下:
1、进入information_schema 数据库(存放了其他的数据库的信息)
use information_schema;

2、查询所有数据的大小:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

3、查看指定数据库的大小:
比如查看数据库home的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';

4、查看指定数据库的某个表的大小
比如查看数据库home中 members 表的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members';

 

https://zhidao.baidu.com/question/543820066.html?qbl=relate_question_0&word=php%20%D4%B4%C2%EB%20%B2%E9%BF%B4%20%B1%ED%20%D5%BC%D3%C3%BF%D5%BC%E4

 

<?php
  $conn = mysql_connect('localhost', 'root', '');
  mysql_select_db('information_schema');
  $target_db_name = 'test';
  $sql = "select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='{$target_db_name}'";
  $result = mysql_query($sql);
  $row = mysql_fetch_row($result);
  print_r($row);

 

在Mysql中会有一个默认的数据库:information_schema,里面有一个Tables表记录了所有表的信息。使用该表来看数据库所占空间大小的代码如下:

USE information_schema;

SELECT TABLE_SCHEMA, SUM(DATA_LENGTH) FROM TABLES GROUP BY TABLE_SCHEMA;

可看到各个数据库的所占空间大小,如果想要看到以k为单位的大小,代码如下:

USE information_schema;

SELECT TABLE_SCHEMA, SUM(DATA_LENGTH)/1024 FROM TABLES GROUP BY TABLE_SCHEMA;

就是字节数除以1024,同理,M和G分别是再除一个1024和再除两个1024.

TABLES表中还有很多其它的数据,有需要的同学可以通过SHOW COLUMNS FROM TABLES查看表的字段。

 

相关推荐

网友评论(0)