加入收藏 | 设为首页 | 会员中心 | 我要投稿 财气旺网 - 财气网 (https://www.caiqiwang.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

查看 MySQL 数据库、表容量大小

发布时间:2022-12-19 14:39:20 所属栏目:MySql教程 来源:转载
导读: 如何在 MySQL 数据库管理中,查询数据库、表、索引的容量大小?我们可以在 MySQL 自带的 information_schema 库中的 Table 表里,找到所需信息。
在每个 MySQL 实例中,都有一个独立的 inf

如何在 MySQL 数据库管理中,查询数据库、表、索引的容量大小?我们可以在 MySQL 自带的 information_schema 库中的 Table 表里,找到所需信息。

在每个 MySQL 实例中,都有一个独立的 information_schema 库,它是自带的默认库MySQL 索引,记录着这个 MySQL 实例中所有数据库的元数据、统计信息、以及有关 MySQL 的访问权限信息。这其中就包括了所有数据库、表、索引的详细信息。

常用 information_schema 库中 Table 表里的字段:

TABLE_SCHEMA : 数据库名

TABLE_NAME:表名

ENGINE:所使用的存储引擎

TABLES_ROWS:记录数

DATA_LENGTH:数据容量大小

INDEX_LENGTH:索引容量大小

有关 information_schema.TABLES 更多字段信息,使用以下命令查看

use information_schema
SHOW COLUMNS FROM TABLES;

更多有关 information_schema 的信息,可查看 MySQL手册

「所有库」的大小

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)',
 sum(truncate(DATA_FREE/1024/1024, 2)) as '碎片占用(MB)'
from 
 information_schema.tables
group by 
 table_schema
order by 
 sum(data_length) desc, sum(index_length) desc;

特别提示:data_length 、index_length 等字段,所存储的容量信息单位是字节,所以我们要除以 2 个 1024 把字节转化为可读性更强的 MB

file

「指定库」的大小

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)',
 sum(truncate(DATA_FREE/1024/1024, 2)) as '碎片占用(MB)'
from 
 information_schema.tables
where 
 table_schema='WUXI_Monitor'
order by 
 data_length desc, index_length desc;

「指定库」中「所有表」的大小

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)',
  truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'
from 
  information_schema.tables
where 
  table_schema='WUXI_Monitor'
order by 
  data_length desc, index_length desc;

「指定库」中「指定表」的大小

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)',
  truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'
from 
  information_schema.tables
where 
  table_schema='WUXI_Monitor' and table_name='warnrecord'
order by 
  data_length desc, index_length desc;

容量排名前 10 的表

USE information_schema;
SELECT 
  TABLE_SCHEMA as '数据库',
  table_name as '表名',
  table_rows as '记录数',
  ENGINE as '存储引擎',
  truncate(data_length/1024/1024, 2) as '数据容量(MB)',
  truncate(index_length/1024/1024, 2) as '索引容量(MB)',
  truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'
from tables 
order by table_rows desc limit 10;

指定库」中,容量排名前 10 的表

USE information_schema;
SELECT 
  TABLE_SCHEMA as '数据库',
  table_name as '表名',
  table_rows as '记录数',
  ENGINE as '存储引擎',
  truncate(data_length/1024/1024, 2) as '数据容量(MB)',
  truncate(index_length/1024/1024, 2) as '索引容量(MB)',
  truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'
from  tables 
where 
  table_schema='WUXI_Monitor' 
order by table_rows desc limit 10;

相关文章

(编辑:财气旺网 - 财气网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!