在我的应用程序中,我的 INSERT 似乎占用了大部分时间。我在内存中有大量对象(~40-50,000),我想将它们插入到表中。
让我们拿一个样本表
CREATE TABLE bill (
id BIGINT(20) PRIMARY KEY,
amount INT(11) DEFAULT 0,
bill_date DATETIME DEFAULT NOW(),
INDEX (bill_date)
) ENGINE=InnoDB
Run Code Online (Sandbox Code Playgroud)
以 3 行作为我的批量大小,以下是我能想到的插入方法
方法 1 - 构建并触发 3 个原始插入
INSERT INTO bill (amount, bill_date) VALUES (10, '2012-01-01 00:00:00');
INSERT INTO bill (amount, bill_date) VALUES (20, '2012-01-02 00:00:00');
INSERT INTO bill (amount, bill_date) VALUES (40, '2013-02-05 00:00:00');
Run Code Online (Sandbox Code Playgroud)
方法 2 - 将值合并到 1 个查询中
INSERT INTO bill (amount, bill_date) VALUES
(10, '2012-01-01 00:00:00'),
(20, '2012-01-02 00:00:00'),
(40, '2013-02-05 …
Run Code Online (Sandbox Code Playgroud)