gpt4 book ai didi

mysql - 在 MySql 中插入 500 条简单的行,耗时超过 40 秒

转载 作者:行者123 更新时间:2023-12-04 19:07:56 25 4
gpt4 key购买 nike

我们有一个既没有外键也没有主键的简单表(对于这个测试)。除了类型为小数(5,4)的 p 之外,所有列都是 int 或 tinyint。
下面是我们正在运行的查询的一部分。完成需要 40 秒。
我们现在从 Phpmyadmin 运行它。但是在测试我们的 Laravel 应用程序时出现了问题,该应用程序花费了太多时间来完成这个简单的任务。
我们的表配置是:
InnoDB - 行格式:动态 - Latin1_Swedish_ci
mysql版本:5.7.31-0ubuntu0.18.04.1
在 Ubuntu 服务器上运行。

DELETE FROM test WHERE species_id = 290;

INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 16);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 15);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 14);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 11);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 17);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 13);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 99999);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 5);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 29);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 21);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 38);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 7);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 30);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 6);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 37);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 40);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 36);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100003);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100008);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100015);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 8);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 2);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 39);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 1);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 42);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 4);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100016);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 28);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 12);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 26);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 24);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 23);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100000);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 25);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 9);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 22);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 18);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 10);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100005);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 20);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100002);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 27);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 19);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 3);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 35);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100004);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100007);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100006);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 34);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 33);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100010);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100011);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100009);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100012);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100014);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 100013);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 32);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 1, 0, 31);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 16);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 15);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 14);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 11);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 17);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 13);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 99999);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 5);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 29);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 21);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 38);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 7);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 30);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 6);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 37);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 40);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 36);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 100003);
INSERT INTO test (species_id, month, p, beta_set_id) VALUES (290, 2, 0, 100008);
...

最佳答案

MySQL 在一次插入一个方面并不是特别快。每个请求都有很多开销。对它们进行批处理,例如:

INSERT INTO test (species_id, month, p, beta_set_id) VALUES
(290, 1, 0, 16),
(290, 1, 0, 15),
(290, 1, 0, 14),
(290, 1, 0, 11),
(290, 1, 0, 17),
(290, 1, 0, 13),
(290, 1, 0, 99999),
(290, 1, 0, 5),
(290, 1, 0, 29),
(290, 1, 0, 21),
(290, 1, 0, 38),
(290, 1, 0, 7),
(290, 1, 0, 30),
(290, 1, 0, 6),
(290, 1, 0, 37),
(290, 1, 0, 40),
(290, 1, 0, 36),
(290, 1, 0, 100003),
(290, 1, 0, 100008),
(290, 1, 0, 100015),
(290, 1, 0, 8),
(290, 1, 0, 2),
(290, 1, 0, 39),
(290, 1, 0, 1),
(290, 1, 0, 42),
(290, 1, 0, 4),
(290, 1, 0, 100016),
(290, 1, 0, 28),
(290, 1, 0, 12),
(290, 1, 0, 26),
(290, 1, 0, 24),
(290, 1, 0, 23),
(290, 1, 0, 100000),
(290, 1, 0, 25),
(290, 1, 0, 9),
(290, 1, 0, 22),
(290, 1, 0, 18),
(290, 1, 0, 10),
(290, 1, 0, 100005),
(290, 1, 0, 20),
(290, 1, 0, 100002),
(290, 1, 0, 27),
(290, 1, 0, 19),
(290, 1, 0, 3),
(290, 1, 0, 35),
(290, 1, 0, 100004),
(290, 1, 0, 100007),
(290, 1, 0, 100006),
(290, 1, 0, 34),
(290, 1, 0, 33),
(290, 1, 0, 100010),
(290, 1, 0, 100011),
(290, 1, 0, 100009),
(290, 1, 0, 100012),
(290, 1, 0, 100014),
(290, 1, 0, 100013),
(290, 1, 0, 32),
(290, 1, 0, 31),
(290, 2, 0, 16),
(290, 2, 0, 15),
(290, 2, 0, 14),
(290, 2, 0, 11),
(290, 2, 0, 17),
(290, 2, 0, 13),
(290, 2, 0, 99999),
(290, 2, 0, 5),
(290, 2, 0, 29),
(290, 2, 0, 21),
(290, 2, 0, 38),
(290, 2, 0, 7),
(290, 2, 0, 30),
(290, 2, 0, 6),
(290, 2, 0, 37),
(290, 2, 0, 40),
(290, 2, 0, 36),
(290, 2, 0, 100003),
(290, 2, 0, 100008);
只要完整的插入语句小于@@max_allowed_pa​​cket(通常至少为一百万),您就可以根据需要插入尽可能多的行。如果需要,以编程方式将其分成 1000 行的批处理。

关于mysql - 在 MySql 中插入 500 条简单的行,耗时超过 40 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64273004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com