如何知道数据库表mysql中单个记录的内存消耗是多少

时间:2017-03-01 04:58:14

标签: mysql

我在DB导出时只有35000 of records的数据库表我发现文件大小超过2 GB。

即使我的表架构也不包含任何BLOB类型的数据。有没有办法在MySQL中识别表行大小。

2 个答案:

答案 0 :(得分:1)

如果我们先通过执行下一个查询的空表找出消耗的内存大小,该怎么办:

SELECT table_name AS "Table",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
WHERE table_schema = "database_name"
AND table_name =  "your_table_name";

然后在表中插入几行数据,然后再次执行上述查询,并计算一条记录的差异和内存消耗的平均值。 例: 我有一张桌子:

create table returning_players
(
casino_id               int not null,
game_id                 int not null,
returning_players_count int null,
primary key (casino_id, game_id)
);

所以空表消耗: enter image description here 然后将1000万条记录插入表:

DROP PROCEDURE IF EXISTS query;
CREATE procedure query()
BEGIN
    declare counter int(20) default 0;
    LABEL:
    LOOP
        IF counter >= 1000000 THEN
            LEAVE LABEL;
        END IF;
        INSERT INTO returning_players VALUES(counter, 45, 5475);
        set counter = counter + 1;
    END LOOP LABEL;
END;
CALL query();

现在我们有内存消耗: enter image description here 如您所见,每1000_000行大约消耗了32.56 MB。从那里我们可以得出一行的消耗量。

答案 1 :(得分:0)

您可以使用此查询来显示表的大小(尽管您需要先替换变量):

SELECT 
table_name AS `Table`, 
round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
WHERE table_schema = "$DB_NAME" 
AND table_name = "$TABLE_NAME";