📜  mysql 数据库大小 - SQL (1)

📅  最后修改于: 2023-12-03 14:44:29.966000             🧑  作者: Mango

MySQL 数据库大小 - SQL

MySQL数据库大小是指占用磁盘的空间大小。在MySQL中,我们可以通过SQL语句来查询数据库的大小信息。下面是一些常用的SQL语句:

1. 查看整个数据库大小
SELECT
    table_schema "数据库名",
    sum(data_length+index_length)/1024/1024 "大小(MB)"
FROM
    information_schema.TABLES
GROUP BY
    table_schema;

以上 SQL 语句将按照数据库名进行分组,获得所有数据库的大小。其中,"data_length" 表示表数据大小,"index_length" 表示所有索引的大小。结果将以MB为单位显示。

2. 查看特定数据库的大小
SELECT
    sum(data_length+index_length)/1024/1024 "大小(MB)"
FROM
    information_schema.TABLES
WHERE
    table_schema='database_name';

以上 SQL 语句将查看名为"database_name"的数据库大小。结果将以MB为单位显示。

3. 查看特定表的大小
SELECT
    table_name "表名",
    round(((data_length+index_length)/1024/1024),2) "大小(MB)"
FROM
    information_schema.TABLES
WHERE
    table_schema = 'database_name'
    AND
    table_name = 'table_name';

以上 SQL 语句将查看名为"database_name"中,名为"table_name"表的大小。结果将以MB为单位显示。

4. 查看所有表的大小并按大小排序
SELECT
    table_name "表名",
    round(((data_length+index_length)/1024/1024),2) "大小(MB)"
FROM
    information_schema.TABLES
WHERE
    table_schema = 'database_name'
ORDER BY
    (data_length+index_length) DESC;

以上 SQL 语句将查看名为"database_name"中所有表的大小,并按照大小排序。结果将以MB为单位显示。

以上是一些常用的查看MySQL数据库大小的SQL语句,可以通过这些语句来确定数据库大小,从而进行存储空间的规划。