写入MySQL数据库3000行。
INSERT INTO test (id, test) VALUES ("1", "test");
INSERT INTO test (id, test) VALUES ("2", "test");
INSERT INTO test (id, test) VALUES ("3", "test");
...
INSERT INTO test (id, test) VALUES ("3000", "test");
如何共享数据库未挂断的部件的请求?
您可以将
多个INSERT
语句分组为一个语句,如下所示。
INSERT INTO test (id, test) VALUES ("1", "test"), ("2", "test"), ("3", "test");
这应该会减少发送到服务器的查询量。
此外,如果它挂断了您的数据库,您可以使用关键字DELAYED
(MySQL 文档)。这将缓存插入,直到数据库有能力插入它们。更直接的查询可能会在插入之前进行,从而延迟它们。这里需要注意的重要一点是,您最终只会将数据包含在表中,而不是立即。
INSERT DELAYED INTO test (id, test) VALUES ("1", "test"), ("2", "test"), ("3", "test");
是不是你有 3000 行要插入但不想把它们全部写出来?如果是这样,请使用以下命令:
$insert = '';
$count = 1;
while ($count <= 3000) {
$insert .= "('$count', 'test'), ";
$count++;
}
$insert = substr($insert, 0, -2);
INSERT INTO test (`id`, `test`) VALUES $insert;
我不确定这是否是您想要的,但是要将多个插入合并到一个查询中,您可以执行以下操作:
INSERT INTO test (id, test) VALUES
("1", "test"),
("2", "test"),
("3", "test");
这应该会大大加快您的脚本速度。