Mysql 查看数据库各个表占用空间

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

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权限。

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、付费专栏及课程。

余额充值