【Mysql】使用mysql语句查看数据库表所占容量空间大小

 

转载自: https://www.jb51.net/database/294882rgk.htm

 

一、查看所有数据库容量大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)'
FROM
    information_schema.TABLES
GROUP BY
    table_schema
ORDER BY
    sum( data_length ) DESC,
    sum( index_length ) DESC;

二、查看所有数据库各表容量大小

1
2
3
4
5
6
7
8
9
10
11
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
ORDER BY
    data_length DESC,
    index_length DESC;

三、查看指定数据库容量大小

1
2
3
4
5
6
7
8
9
10
11
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)'
FROM
    information_schema.TABLES
WHERE
    table_schema = 'osale_im';

四、查看指定数据库各表容量大小

1
2
3
4
5
6
7
8
9
10
11
12
13
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 = 'osale_im'
ORDER BY
    data_length DESC,
    index_length DESC;

 

五:查看指定数据库指定表容量大小

六. 查看所有产生碎片的表

1
2
3
4
5
6
7
8
  SELECT table_schema db,
         table_name,
         data_free,
         engine
    FROM information_schema.tables
   WHERE table_schema NOT IN ('information_schema', 'mysql')
     AND data_free > 0
ORDER BY DATA_FREE DESC;

查看某个表的碎片大小

SHOW TABLE STATUS LIKE '表名';

查询结果中的'Data_free'字段的值就是碎片大小。

七. 清理表碎片

1
2
3
4
5
6
7
/*1. MyISAM表*/
  
OPTIMIZE TABLE 表名
  
/*2. InnoDB表*/
  
ALTER TABLE 表名 engine = InnoDB

附:sql语句查询到整个数据库的容量

在需要备份数据库里面的数据时,我们需要知道数据库占用了多少磁盘大小,可以通过一些sql语句查询到整个数据库的容量,也可以单独查看表所占容量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1、要查询表所占的容量,就是把表的数据和索引加起来就可以了
select sum(DATA_LENGTH)+sum(INDEX_LENGTH) from information_schema.tables
where table_schema='数据库名';
  上面获取的结果是以字节为单位的,可以通过%1024在%1024的到M为单位的结果。
  2、查询所有的数据大小
select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from tables; -- 查询所有的数据大小
  3、查询某个表的数据
select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from tables where table_schema='数据库名' AND table_name='表名';
1Byte = 8 bits;
1KB = 1024Byte = 2的10次方Byte  = 1024 Byte
1MB = 1024KB   = 2的20次方Byte  = 1048576 Byte
1GB = 1024MB   = 2的30次方Byte  = 1073741824 Byte
1TB = 1024GB   = 2的40次方Byte  = 1099511627776 Byte
1PB = 1024TB   = 2的50次方Byte  = 1125899906842624 Byte
1EB = 1024PB   = 2的60次方Byte  = 1152921504606846976 Byte
1ZB = 1024EB   = 2的70次方Byte  = 1180591620717411303424 Byte
1YB = 1024ZB   = 2的80次方Byte  = 1208925819614629174706176 Byte
1DB = 1024YB   = 2的90次方Byte  = 1237940039285380274899124224 Byte
1NB = 1024DB   = 2的100次方Byte = 1267650600228229401496703205376 Byte
在mysql中有一个information_schema数据库,这个数据库中装的是mysql的元数据,包括数据库信息、数据库中表的信息等。所以要想查询数据库占用磁盘的空间大小可以通
  过对information_schema数据库进行操作。
information_schema中的表主要有:
  schemata表:这个表里面主要是存储在mysql中的所有的数据库的信息
  tables表:这个表里存储了所有数据库中的表的信息,包括每个表有多少个列等信息。
  columns表:这个表存储了所有表中的表字段信息。
  statistics表:存储了表中索引的信息。
  user_privileges表:存储了用户的权限信息。
  schema_privileges表:存储了数据库权限。
  table_privileges表:存储了表的权限。
  column_privileges表:存储了列的权限信息。
  character_sets表:存储了mysql可以用的字符集的信息。
  collations表:提供各个字符集的对照信息。
  collation_character_set_applicability表:相当于collations表和character_sets表的前两个字段的一个对比,记录了字符集之间的对照信息。
  table_constraints表:这个表主要是用于记录表的描述存在约束的表和约束类型。
  key_column_usage表:记录具有约束的列。
  routines表:记录了存储过程和函数的信息,不包含自定义的过程或函数信息。
  views表:记录了视图信息,需要有show view权限。
  triggers表:存储了触发器的信息,需要有super权限。

 

posted @ 2024-03-28 14:59  Angel挤一挤  阅读(866)  评论(0编辑  收藏  举报