MySQL数据库的数据大小可以通过以下方法来查看:
SELECT table_schema AS '数据库名',
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS '大小(MB)'
FROM information_schema.tables
GROUP BY table_schema;
这将显示每个数据库的总大小(包括数据和索引)。
DELIMITER //
CREATE PROCEDURE `GetDatabaseSize`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE schema_name VARCHAR(100);
DECLARE cur CURSOR FOR
SELECT schema_name FROM information_schema.schemata;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
CREATE TEMPORARY TABLE IF NOT EXISTS db_size (
schema_name VARCHAR(100),
size_mb FLOAT
);
OPEN cur;
read_loop: LOOP
FETCH cur INTO schema_name;
IF done THEN
LEAVE read_loop;
END IF;
SET @query = CONCAT('SELECT "', schema_name, '" AS schema_name,
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS size_mb
FROM ', schema_name, '.table_name');
PREPARE stmt FROM @query;
EXECUTE stmt;
INSERT INTO db_size (schema_name, size_mb)
SELECT schema_name, size_mb;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
SELECT * FROM db_size;
DROP TABLE db_size;
END //
DELIMITER ;
执行以上存储过程后,可以调用GetDatabaseSize
存储过程来获取所有数据库的大小。
腾讯云的关联产品和介绍链接如下:
请注意,本答案所提及的产品和链接仅作为示例,仅供参考。具体选择适合自己需求的产品,建议根据实际情况进行评估和选择。
领取专属 10元无门槛券
手把手带您无忧上云