【MySQL】查询指定数据库各个表的记录数、数据容量、索引容量

本文档介绍了如何使用SQL查询MySQL数据库中各表的记录数、数据容量和索引容量,通过'information_schema.tables'表实现,同时统计了表的数量。重点在于数据库管理和维护的实践技巧。

一. 概述

查看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)'
from information_schema.tables
where table_schema='dbname'
order by table_rows desc, index_length desc;

三. 查询表数量

select count(table_name)
from information_schema.tables
where table_schema='dbname'

MySQL 中,可使用以下几种方法查询数据库占用空间: - 查询数据库中所有数据占用空间: ```sql SELECT concat( round( sum( DATA_LENGTH / 1024 / 1024 ), 2 ), 'MB' ) AS DATA FROM TABLES; ``` - 查询指定数据库指定数据占用空间: ```sql SELECT concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS DATA FROM TABLES WHERE table_schema = '{your database}' AND table_name = '{your table name}'; ``` - 查询指定数据库指定索引占用空间: ```sql SELECT concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS DATA FROM TABLES WHERE table_schema = '{your database}' AND table_name = '{your table name}'; ``` - 查看某个数据库各个占用的空间(以 MB 为单位),包含记录数数据容量索引容量: ```sql 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='mysql' order by data_length desc, index_length desc; ``` - 查询指定数据库数据大小、索引大小总大小: ```sql SELECT round(sum(data_length / 1024 / 1024),2) 'Data Size in MB',round(sum(index_length / 1024 / 1024),2) 'Index Size in MB',round(sum((index_length + data_length) / 1024 / 1024),2) 'All Size in MB' FROM information_schema.TABLES WHERE table_schema = '库名'; ``` 上述代码分别来自不同的引用,具体引用信息如下:第一种方法来自引用 [1],第二种方法来自引用 [1],第三种方法来自引用 [1],第四种方法来自引用 [2],第五种方法来自引用 [4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值