本文永久链接: https://www.xtplayer.cn/mysql/how-to-get-the-sizes-of-the-tables-of-a-mysql-database/

查看 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_lengthindex_length 等字段,所存储的容量信息单位是字节,所以我们要除以 2 个 1024 把字节转化为可读性更强的 MB。

查看 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
where table_schema='<数据库名>'
order by data_length desc, index_length desc;

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

注意:请将代码中 ‘kalacloud_test_data‘ 数据库名改为你要查询的数据库名。

查看 MySQL「指定库」中「所有表」的容量大小

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='<数据库名>'
order by
data_length desc, index_length desc;

注意:请将代码中 ‘kalacloud_test_data‘ 数据库名改为你要查询的数据库名。

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

查看 MySQL「指定库」中「指定表」的容量大小

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='<数据库名>' and table_name='<表名>'
order by
data_length desc, index_length desc;

注意:请将代码中 kalacloud_test_data 数据库名改为你要查询的数据库名,product_demo 改为你要查询的表名。

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

查看 MySQL 数据库中,容量排名前 10 的表

首先,先进入 information_schema 库里,然后执行以下命令:

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;

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

查看 MySQL「指定库」中,容量排名前 10 的表

我们先进入 information_schema 库里,再执行以下命令:

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='<数据库名>'
order by table_rows desc limit 10;

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

统计单个表记录条数

use '<数据库名>';
select count(*) from <表名>;
# 计算具有某个字段值的记录条数
select count(*) from <表名> where operation="GET";

删除具有某个字段值的记录

DELETE FROM audit_log_parsed where operation="GET" LIMIT 10000;