查看Mysql数据库大小

查看Mysql数据库大小

𝓓𝓸𝓷 Lv6

查看Mysql数据库占用空间大小

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---统计各个数据库数据总量
SELECT
table_schema AS "Database",
SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)"
FROM
information_schema.TABLES
GROUP BY
table_schema;

---查看数据库中各个表大小
SELECT table_schema AS 'Database',
table_name AS 'Table',
round(((data_length + index_length) / 1024 / 1024), 2) AS 'Size_MB'
FROM information_schema.tables
ORDER BY data_length + index_length DESC;

---查询各个数据库中的数据记录数、数据量、索引容量
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;

---查看指定数据库大小
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 = 'test';

---查看指定数据库各个表的容量
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 = 'test'
ORDER BY
data_length DESC,
index_length DESC;

---查看指定数据库各表中列数量
SELECT TABLE_NAME, COUNT(*) AS COLUMN_COUNT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'test'
GROUP BY TABLE_NAME;

---查看数据库表空间碎片大小
SELECT
table_schema AS `Database`,
table_name AS `Table`,
round(((data_length + index_length) / 1024 / 1024), 2) AS `Size (MB)`,
round((data_free / 1024 / 1024), 2) AS `Free Space (MB)`
FROM
information_schema.TABLES
WHERE
table_schema='填数据库名';
  • Title: 查看Mysql数据库大小
  • Author: 𝓓𝓸𝓷
  • Created at : 2025-01-14 16:30:37
  • Updated at : 2025-01-15 18:14:34
  • Link: https://www.zhangdong.me/mysql-database-size.html
  • License: This work is licensed under CC BY-NC-SA 4.0.
评论