DBA技术分享(十三)- 查询mysql的Data size

一、概述

日常工作中查询mysql的Data size是最常用的查询,下面分享以下几个常用的使用方法

二、相关SQL

2.1 按表的大小

select table_schema as database_name,    table_name,    round(sum((data_length + index_length)) / power(1024, 2), 2) as used_mb,    round(sum((data_length + index_length + data_free)) /          power(1024, 2), 2) as allocated_mbfrom information_schema.tableswhere table_schema = 'your database name' -- put your database name here    and table_type = 'BASE TABLE'group by table_schema,         table_nameorder by used_mb desc;

说明:

  • database_name - 表数据库(模式)名称
  • table_name - 表名
  • used_mb - 以 MB 为单位的已用空间
  • allowed_mb - 以 MB 为单位分配的空间

2.2 MySQL数据库中按数据和索引大小列出表

select table_schema as database_name,       table_name,       round(1.0*data_length/1024/1024, 2) as data_size,       round(index_length/1024/1024, 2) as index_size,       round((data_length + index_length)/1024/1024, 2) as total_sizefrom information_schema.tableswhere table_schema not in('information_schema', 'mysql',                          'sys', 'performance_schema')      -- and table_schema = 'your database name'order by total_size desc;

说明:

  • database_name - 数据库(模式)名称
  • table_name - 表名
  • data_size - 以兆字节为单位的数据大小
  • index_size - 表所有索引的大小,以兆字节为单位
  • total_size - 总大小、数据和索引

2.3 在 MySQL 数据库中按大小列出 idex

select database_name,       index_name,       (1.0*stat_value*@@innodb_page_size/1024/1024) as index_size,       table_namefrom mysql.innodb_index_statswhere stat_name = 'size'      and index_name not in ('PRIMARY', 'GEN_CLUST_INDEX')      -- and database_name = 'put your database name here'order by index_size desc;

说明:

  • database_name - 数据库的名称(模式)
  • index_name - 索引名称
  • index_size - 按索引使用的空间(以 MB 为单位)
  • table_name - 表的名称

2.4 列出 MySQL 数据库中最大的 10 个表

select table_schema as database_name,       table_name,       round( (data_length + index_length) / 1024 / 1024, 2)  as total_size,       round( (data_length) / 1024 / 1024, 2)  as data_size,       round( (index_length) / 1024 / 1024, 2)  as index_sizefrom information_schema.tableswhere table_schema not in ('information_schema', 'mysql',                           'performance_schema' ,'sys')      and table_type = 'BASE TABLE'      -- and table_schema = 'your database name'order by total_size desclimit 10;

说明:

  • database_name - 表的模式名称
  • table_name - 表名
  • total_size - 总表大小
  • data_size - 表行的大小
  • index_size - 索引的大小

小结

后面会分享更多精彩内容,感兴趣的朋友可以关注下!

技术   DBA
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章