我使用的是Php 5.4,mySql 5.6
我有一个架构,我需要在一个查询中填充100000个条目(用于测试目的)。我有一些问题因为服务器需要很长时间才能详细说明,所以它达到了详细说明的时间限制。或者它直接无法调用数据库,发送一般致命错误消息而无需进一步解释。
我的问题很简单:为什么?是否有一些必须更改的PHP参数?
此外,最好是运行1000000次的单个插入或1000000个记录的单个多插入查询?
P.S。:如果我循环13次单个实例,其中插入8000个记录,那就没事了,它不会花这么长时间......
答案 0 :(得分:0)
你可以这样做,例如:
<?php
$x=1;
$col1 = 1;
$col2 = 1;
$col3 = 1;
$quantity = 1000000-1;
$query = "INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES ('$col1', '$col2', '$col3') ";
while ($x <= $quantity)
{
$col1++;
$col2++;
$col3++;
$x++;
$query .= ", ('$col1', '$col2', '$col3') ";
}
echo $query;
?>
将数量更改为100-1
会产生以下结果:
INSERT INTO`table`(`col1`,`col2`,`col3`)VALUES(&#39; 1&#39;,&#39; 1&#39;,&#39; 1&#39;) , (&#39; 2&#39;,&#39; 2&#39;,&#39; 2&#39;),(&#39; 3&#39;,&#39; 3&#39;,&# 39; 3&#39;),(&#39; 4&#39;,&#39; 4&#39;,&#39; 4&#39;),(&#39; 5&#39;,&#39; ; 5&#39;,&#39; 5&#39;) ,(&#39; 6&#39;,&#39; 6&#39;,&#39; 6&#39;),(&#39; 7&#39;,&#39; 7&#39;,& #39; 7&#39;),(&#39; 8&#39;,&#39; 8&#39;,&#39; 8&#39;),(&#39; 9&#39;,&# 39; 9&#39 ;, &#39; 9&#39;),(&#39; 10&#39;,&#39; 10&#39;,&#39; 10&#39;),(&#39; 11&#39;,& #39; 11&#39;,&#39; 11&#39;),(&#39; 12&#39;,&#39; 12&#39;,&#39; 12&#39;), (&#39; 13&#39;,&#39; 13&#39;,&#39; 13&#39;),(&#39; 14&#39;,&#39; 14&#39;,&# 39; 14&#39;),(&#39; 15&#39;,&#39; 15&#39;,&#39; 15&#39;),(&#39; 16&#39;, &#39; 16&#39;,&#39; 16&#39;),(&#39; 17&#39;,&#39; 17&#39;,&#39; 17&#39;),(& #39; 18&#39;,&#39; 18&#39;,&#39; 18&#39;),(&#39; 19&#39;,&#39; 19&#39;, &#39; 19&#39;),(&#39; 20&#39;,&#39; 20&#39;,&#39; 20&#39;),(&#39; 21&#39;,& #39; 21&#39;,&#39; 21&#39;),(&#39; 22&#39;,&#39; 22&#39;,&#39; 22&#39;), (&#39; 23&#39;,&#39; 23&#39;,&#39; 23&#39;),(&#39; 24&#39;,&#39; 24&#39;,&# 39; 24&#39;),(&#39; 25&#39;,&#39; 25&#39;,&#39; 25&#39;),(&#39; 26&#39;, &#39; 26&#39;,&#39; 26&#39;),(&#39; 27&#39;,&#39; 27&#39;,&#39; 27&#39;),(& #39; 28&#39;,&#39; 28&#39;,&#39; 28&#39;),(&#39; 29&#39;,&#39;&#39;, &#39; 29&#39;),(&#39; 30&#39;,&#39; 30&#39;,&#39; 30&#39;),(&#39; 31&#39;,& #39; 31&#39;,&#39; 31&#39;),(&#39; 32&#39;,&#39; 32&#39;,&#39; 32&#39;), (&#39; 33&#39;,&#39; 33&#39;,&#39; 33&#39;),(&#39; 34&#39;,&#39; 34&#39;,&# 39; 34&#39;),(&#39; 35&#39;,&#39; 35&#39;,&#39; 35&#39;),(&#39; 36&#39;, &#39; 36&#39;,&#39; 36&#39;),(&#39; 37&#39;,&#39; 37&#39;,&#39; 37&#39;),(& #39; 38&#39;,&#39; 38&#39;,&#39; 38&#39;),(&#39; 39&#39;,&#39; 39&#39;, &#39; 39&#39;),(&#39; 40&#39;,&#39; 40&#39;,&#39; 40&#39;),(&#39; 41&#39;,& #39; 41&#39;,&#39; 41&#39;),(&#39; 42&#39;,&#39; 42&#39;,&#39; 42&#39;), (&#39; 43&#39;,&#39; 43&#39;,&#39; 43&#39;),(&#39; 44&#39;,&#39; 44&#39;,&# 39; 44&#39;),(&#39; 45&#39;,&#39; 45&#39;,&#39; 45&#39;),(&#39; 46&#39;, &#39; 46&#39;,#39; 46&#39;),(&#39; 47&#39;,&#39; 47&#39;,&#39; 47&#39;),(& #39; 48&#39;,&#39; 48&#39;,&#39; 48&#39;),(&#39; 49&#39;,&#39; 49&#39;, &#39; 49&#39;),(&#39; 50&#39;,&#39; 50&#39;,&#39; 50&#39;),(&#39; 51&#39;,& #39; 51&#39;,&#39; 51&#39;),(&#39; 52&#39;,&#39; 52&#39;,&#39; 52&#39;), (&#39; 53&#39;,&#39; 53&#39;,&#39; 53&#39;),(&#39; 54&#39;,&#39; 54&#39;,&# 39; 54&#39;),(&#39; 55&#39;,&#39; 55&#39;,&#39; 55&#39;),(&#39; 56&#39;, &#39; 56&#39;,&#39; 56&#39;),(&#39; 57&#39;,&#39; 57&#39;,&#39; 57&#39;),(& #39; 58&#39;,&#39; 58&#39;,&#39; 58&#39;),(&#39; 59&#39;,&#39; 59&#39;, &#39; 59&#39;),(&#39; 60&#39;,&#39; 60&#39;,&#39; 60&#39;),(&#39; 61&#39;,& #39; 61&#39;,&#39; 61&#39;),(&#39; 62&#39;,&#39; 62&#39;,&#39; 62&#39;), (&#39; 63&#39;,&#39; 63&#39;,&#39; 63&#39;),(&#39; 64&#39;,&#39; 64&#39;,&# 39; 64&#39;),(&#39; 65&#39;,&#39; 65&#39;,&#39; 65&#39;),(&#39; 66&#39;, &#39; 66&#39;,&#39; 66&#39;),(&#39; 67&#39;,&#39; 67&#39;,&#39; 67&#39;),(& #39; 68&#39;,&#39; 68&#39;,&#39; 68&#39;),(&#39; 69&#39;,&#39; 69&#39;, &#39; 69&#39;),(&#39; 70&#39;,&#39; 70&#39;,&#39; 70&#39;),(&#39; 71&#39;,& #39; 71&#39;,&#39; 71&#39;),(&#39; 72&#39;,&#39; 72&#39;,&#39; 72&#39;), (&#39; 73&#39;,&#39; 73&#39;,&#39; 73&#39;),(&#39; 74&#39;,&#39; 74&#39;,&# 39; 74&#39;),(&#39; 75&#39;,&#39; 75&#39;,&#39; 75&#39;),(&#39; 76&#39;, &#39; 76&#39;,&#39; 76&#39;),(&#39; 77&#39;,&#39; 77&#39;,&#39; 77&#39;),(& #39; 78&#39;,&#39; 78&#39;,&#39; 78&#39;),(&#39; 79&#39;,&#39; 79&#39;, &#39; 79&#39;),(&#39; 80&#39;,&#39; 80&#39;,&#39; 80&#39;),(&#39; 81&#39;,& #39; 81&#39;,&#39; 81&#39;),(&#39; 82&#39;,&#39; 82&#39;,&#39; 82&#39;), (&#39; 83&#39;,&#39; 83&#39;,&#39; 83&#39;),(&#39; 84&#39;,&#39; 84&#39;,&# 39; 84&#39;),(&#39; 85&#39;,&#39; 85&#39;,&#39; 85&#39;),(&#39; 86&#39;, &#39; 86&#39;,&#39; 86&#39;),(&#39; 87&#39;,&#39; 87&#39;,&#39; 87&#39;),(& #39; 88&#39;,&#39; 88&#39;,&#39; 88&#39;),(&#39; 89&#39;,&#39; 89&#39;, &#39; 89&#39;),(&#39; 90&#39;,&#39; 90&#39;,&#39; 90&#39;),(&#39; 91&#39;,& #39; 91&#39;,&#39; 91&#39;),(&#39; 92&#39;,&#39; 92&#39;,&#39; 92&#39;), (&#39; 93&#39;,&#39; 93&#39;,&#39; 93&#39;),(&#39; 94&#39;,&#39; 94&#39;,&# 39; 94&#39;),(&#39; 95&#39;,&#39; 95&#39;,&#39; 95&#39;),(&#39; 96&#39;, &#39; 96&#39;,&#39; 96&#39;),(&#39; 97&#39;,&#39; 97&#39;,&#39; 97&#39;),(& #39; 98&#39;,&#39; 98&#39;,&#39; 98&#39;),(&#39; 99&#39;,&#39; 99&#39;, &#39; 99&#39;),(&#39; 100&#39;,&#39; 100&#39;,&#39; 100&#39;)
没有建议这样做。它需要一段时间才能运行,我不确定它是否可行,您可以增加页面的超时以确保它运行。
但是因为它用于测试目的..
如果需要,您可以更改col变量的值,但不能在字符串上使用$var++
。
答案 1 :(得分:0)
问题是,在杀死进程之前,PHP允许的测试时间超过30秒左右?
如果是,您可以在max_execution_time
中设置php.ini
。
见
答案 2 :(得分:0)
您可以尝试最小化应用程序服务器和SQL引擎之间的流量:
首先插入一条记录,然后在PHP循环中,将每次迭代中的记录数加倍:
第一条记录:
INSERT INTO TABLE mytable (txt) VALUES ('Test');
循环:
INSERT INTO TABLE mytable (txt)
SELECT txt FROM mytable;
如果你执行这个循环20次,你最终会得到2个 20 记录,即~1000000
如果你有一个AUTO_INCREMENT
id字段,它当然会填充唯一值。您还可以显式增加另一个数字字段,如下所示:
INSERT INTO TABLE mytable (num) VALUES (1);
在循环(PHP)中:
$count = 1;
foreach (range(1, 20) as $i) {
$sql = "INSERT INTO TABLE mytable (num)
SELECT num + $count FROM mytable";
$mysqli->query($sql);
$count += $count;
}
答案 3 :(得分:-1)
您应该逐一插入此内容,以获得最佳性能并获得更清晰的声明。您还应该删除任何索引或约束,以及使用MyISAM而不是innodb来获得最快的sql插入。最好是使用.sql文件和控制台输入。