在 MySql 中插入 500 行简单行需要 40 多秒
Insertion of 500 simple rows taking over 40 seconds in MySql
我们有一个既没有外键也没有主键的简单 table(用于此测试)。除了类型为 decimal(5,4).
的 p 外,所有列都是 int 或 tinyint
下面是我们运行的部分查询。完成需要 40 秒。
我们现在 运行 来自 Phpmyadmin。但是在测试我们的 Laravel 应用程序时出现了问题,该应用程序花费了太多时间来完成这个简单的任务。
我们的table配置是:
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_packet(通常至少一百万),您可以根据需要插入任意数量的行。如果需要,以编程方式将其分成 1000 行的批次。
我们有一个既没有外键也没有主键的简单 table(用于此测试)。除了类型为 decimal(5,4).
的 p 外,所有列都是 int 或 tinyint下面是我们运行的部分查询。完成需要 40 秒。
我们现在 运行 来自 Phpmyadmin。但是在测试我们的 Laravel 应用程序时出现了问题,该应用程序花费了太多时间来完成这个简单的任务。
我们的table配置是: 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_packet(通常至少一百万),您可以根据需要插入任意数量的行。如果需要,以编程方式将其分成 1000 行的批次。