INSERT INTO ndbtable SELECT FROM 表在 MySQL 集群中使用了荒谬的内存量

Posted

技术标签:

【中文标题】INSERT INTO ndbtable SELECT FROM 表在 MySQL 集群中使用了荒谬的内存量【英文标题】:INSERT INTO ndbtable SELECT FROM table uses ridiculous amounts of memory in MySQL Cluster 【发布时间】:2013-10-24 03:04:57 【问题描述】:

如果我将 300,000 行从表(innodb 或 ndb)移动到 ndb 表中,如下所示:

INSERT INTO ndbtable2
SELECT a, b,IFNULL(c,UTC_TIMESTAMP()),CASE WHEN b = 'X' THEN e ELSE d END,f
FROM ndbtable1;

Query OK, 308372 rows affected (5 min 12.59 sec)
Records: 308372  Duplicates: 0  Warnings: 0

ndb 在完成之前使用越来越多的数据内存。前/峰/后如下

ndb_mgm -e "all report memoryusage"
Connected to Management Server at: fl-prd-mysql1:1186
Node 1: Data usage is 2%(5752 32K pages of total 262144)
Node 1: Index usage is 0%(2428 8K pages of total 262176)
Node 2: Data usage is 2%(5752 32K pages of total 262144)
Node 2: Index usage is 0%(2428 8K pages of total 262176)

Connected to Management Server at: fl-prd-mysql1:1186
Node 1: Data usage is 62%(164013 32K pages of total 262144)
Node 1: Index usage is 1%(3136 8K pages of total 262176)
Node 2: Data usage is 62%(164013 32K pages of total 262144)
Node 2: Index usage is 1%(3136 8K pages of total 262176)

Connected to Management Server at: fl-prd-mysql1:1186
Node 1: Data usage is 3%(10293 32K pages of total 262144)
Node 1: Index usage is 1%(4590 8K pages of total 262176)
Node 2: Data usage is 3%(10293 32K pages of total 262144)
Node 2: Index usage is 1%(4590 8K pages of total 262176)

如果我的数学是正确的,我插入 10293 - 5752 = 4541 = 142 MB,但内存峰值为 164013 - 5752 = 158261 = 4945 MB

此外,如果我将插入限制为 50,000 行,前后差异只有 3MB,那么峰值为 780MB。

显然,当 ndb 不为空时,这将是一个问题……这里发生了什么?!

【问题讨论】:

【参考方案1】:

在没有解释的情况下,手册确实承认以下限制

正如本章其他部分所述,MySQL Cluster 不能很好地处理大型事务;与尝试包含大量操作的单个大事务相比,最好执行多个小事务,每个事务具有少量操作。除其他考虑因素外,大型事务需要非常大量的内存

http://dev.mysql.com/doc/refman/5.5/en/mysql-cluster-limitations-transactions.html

“正如本章其他地方所指出的” ...我已经看过但还没有找到任何具体的内容。

仅供参考,替代方案是:

    使用一行 LIMIT(比如 10,000)循环遍历迭代 如果插入空表,请考虑 LOAD DATA 甚至使用 innodb,最后使用 ALTER TABLE ... ENGINE = ndbcluster(两者都不是事务性的)

【讨论】:

以上是关于INSERT INTO ndbtable SELECT FROM 表在 MySQL 集群中使用了荒谬的内存量的主要内容,如果未能解决你的问题,请参考以下文章

Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS

insert into 语句错误

易语言 insert into

insert into 插入数据问题在线等

VB insert into 的用法

sql [INSERT INTO] #Syntax #Insert_into