Answers:
这是针对INFORMATION_SCHEMA的更有条理的查询
SELECT
IFNULL(B.engine, 'Total') "Storage Engine",
CONCAT(LPAD(REPLACE(FORMAT(B.DSize / POWER(1024, pw), 3), ',', ''), 17, ' '), ' ', SUBSTR(' KMGTP', pw + 1, 1), 'B') "Data Size",
CONCAT(LPAD(REPLACE( FORMAT(B.ISize / POWER(1024, pw), 3), ',', ''), 17, ' '), ' ', SUBSTR(' KMGTP', pw + 1, 1), 'B') "Index Size",
CONCAT(LPAD(REPLACE( FORMAT(B.TSize / POWER(1024, pw), 3), ',', ''), 17, ' '), ' ', SUBSTR(' KMGTP', pw + 1, 1), 'B') "Table Size"
FROM
(SELECT
engine,
SUM(data_length) DSize,
SUM(index_length) ISize,
SUM(data_length + index_length) TSize
FROM
information_schema.tables
WHERE
table_schema NOT IN ('mysql', 'information_schema', 'performance_schema')
AND engine IS NOT NULL
GROUP BY engine WITH ROLLUP
) B,
(SELECT 3 pw) A
ORDER BY TSize;
SELECT
dbname,
Concat(Lpad(Format(sdsize / Power(1024, pw), 3), 17, ' '), ' ', Substr(' KMGTP', pw + 1, 1), 'B') "Data Size",
Concat(Lpad(Format(sxsize / Power(1024, pw), 3), 17, ' '), ' ', Substr(' KMGTP', pw + 1, 1), 'B') "Index Size",
Concat(Lpad(Format(stsize / Power(1024, pw), 3), 17, ' '), ' ', Substr(' KMGTP', pw + 1, 1), 'B') "Total Size"
FROM
(SELECT
Ifnull(db, 'All Databases') DBName,
Sum(dsize) SDSize,
Sum(xsize) SXSize,
Sum(tsize) STSize
FROM (SELECT
table_schema DB,
data_length DSize,
index_length XSize,
data_length + index_length TSize
FROM information_schema.tables
WHERE table_schema NOT IN ('mysql','information_schema','performance_schema')
) AAA
GROUP BY db WITH rollup
) AA,
(SELECT 3 pw) BB
ORDER BY ( sdsize + sxsize );
SELECT
Statistic,
DataSize "Data Size",
IndexSize "Index Size",
TableSize "Table Size"
FROM
(SELECT
IF(ISNULL(table_schema) = 1, 10, 0) schema_score,
IF(ISNULL(engine) = 1, 10, 0) engine_score,
IF(ISNULL(table_schema) = 1, 'ZZZZZZZZZZZZZZZZ', table_schema) schemaname,
IF(ISNULL(B.table_schema) + ISNULL(B.engine) = 2, "Storage for All Databases", IF(ISNULL(B.table_schema) + ISNULL(B.engine) = 1, CONCAT("Storage for ", B.table_schema), CONCAT(B.engine, " Tables for ", B.table_schema))) Statistic,
CONCAT(LPAD(REPLACE(FORMAT(B.DSize / POWER(1024, pw), 3), ',', ''), 17, ' '), ' ', SUBSTR(' KMGTP', pw + 1, 1), 'B') DataSize,
CONCAT(LPAD(REPLACE( FORMAT(B.ISize / POWER(1024, pw), 3), ',', ''), 17, ' '), ' ', SUBSTR(' KMGTP', pw + 1, 1), 'B') IndexSize,
CONCAT(LPAD(REPLACE(FORMAT(B.TSize / POWER(1024, pw), 3), ',', ''), 17, ' '), ' ', SUBSTR(' KMGTP', pw + 1, 1), 'B') TableSize
FROM
(SELECT
table_schema,
engine,
SUM(data_length) DSize,
SUM(index_length) ISize,
SUM(data_length + index_length) TSize
FROM
information_schema.tables
WHERE
table_schema NOT IN ('mysql', 'information_schema', 'performance_schema')
AND engine IS NOT NULL
GROUP BY
table_schema, engine WITH ROLLUP
) B,
(SELECT 3 pw) A
) AA
ORDER BY schemaname, schema_score, engine_score;
在三个(3)查询中的每个查询中,您都会看到(SELECT 3 pw)
。该pw
代表电源1024来显示特定单位的结果:
(SELECT 0 pw)
将以字节显示报告(SELECT 1 pw)
将以千字节为单位显示报告(SELECT 2 pw)
将以兆字节显示报告(SELECT 3 pw)
将以千兆字节显示报告(SELECT 4 pw)
将以兆字节为单位显示报告(SELECT 5 pw)
将以PetaBytes显示报告(如果运行此报告,请与我联系)这是一个报表查询,其格式略有减少KB
:
SELECT
IFNULL(db, 'Total') "Database",
datsum / power(1024, pw) "Data Size",
ndxsum / power(1024, pw) "Index Size",
totsum / power(1024, pw) "Total"
FROM
(
SELECT
db,
SUM(dat) datsum,
SUM(ndx) ndxsum,
SUM(dat + ndx) totsum
FROM
(
SELECT table_schema db, data_length dat, index_length ndx
FROM information_schema.tables
WHERE engine IS NOT NULL AND table_schema NOT IN ('information_schema', 'mysql')
) AA
GROUP BY db WITH ROLLUP
) A,
(SELECT 1 pw) B;
我终于找到了简单的方法,只需单击几下即可直接从亚马逊获取此信息。
show table status from mydatabsename;
其中mydatabasename是您的数据库名称。
这将显示每个表的指标Data_length和Index_length以及其他指标。您必须总计这些列,并记住它们以字节为单位,因此您必须除以1024以获取kb,然后再除以1024以获取兆,然后再除以1024以获取gig。这也显示了索引/数据库分配中的可用空间。
如果您想探索,可以得到更详细的信息和sum():http : //dev.mysql.com/doc/refman/5.5/en/show-table-status.html
SELECT SUM(DATA_FREE) FROM INFORMATION_SCHEMA.PARTITIONS;
显示索引/数据库分配中的剩余空间...
SELECT SUM(Data_length) FROM INFORMATION_SCHEMA.PARTITIONS;
SELECT SUM(Index_length) FROM INFORMATION_SCHEMA.PARTITIONS;
...显示使用的数据和索引大小(您必须添加它们以进行总分配)
如果您想进一步剖析...
select sum(Data_length) from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMA = "myschema";
select sum(Index_length) from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMA = "myschema";
select sum(DATA_FREE) from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMA = "myschema";
select sum(Data_length) from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMA = "myschema" and TABLE_NAME = "aspecifictable";
select sum(Index_length) from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMA = "myschema" and TABLE_NAME = "aspecifictable";
当然,您也可以像我在这里找到的那样使用MySQL Workbench:http://dev.mysql.com/downloads/tools/workbench/,但这假定您具有对数据库服务器的端口访问权限。不过,您也可以离线进行很多操作,因此值得下载。请注意,工作台不会对我没有意义的sum()分配。但是话又说回来,我也没有最新版本。
2019年:对于MySQL和MariaDB,请参阅此AWS提供的链接:https : //aws.amazon.com/premiumsupport/knowledge-center/view-storage-rds-mysql-mariadb/