I m having a strange time dealing with selecting from a table with about 30,000 rows.
It seems my script is using an outrageous amount of memory for what is a simple, forward only walk over a query result.
Please note that this example is a somewhat contrived, absolute bare minimum example which bears very little resemblance to the real code and it cannot be replaced with a simple database aggregation. It is intended to illustrate the point that each row does not need to be retained on each iteration.
<?php
$pdo = new PDO( mysql:host=127.0.0.1 , foo , bar , array(
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
));
$stmt = $pdo->prepare( SELECT * FROM round );
$stmt->execute();
function do_stuff($row) {}
$c = 0;
while ($row = $stmt->fetch()) {
// do something with the object that doesn t involve keeping
// it around and can t be done in SQL
do_stuff($row);
$row = null;
++$c;
}
var_dump($c);
var_dump(memory_get_usage());
var_dump(memory_get_peak_usage());
产出:
int(39508)
int(43005064)
int(43018120)
我不理解为什么在几乎不需要随时保存任何数据时,会使用40只记忆。 我已经做了大量工作,用大约6个因素来减少记忆,将“选举后”改为“选举后回家”,但我甚至认为,这种使用率很低,而且表格只会增加。
Is there a setting I m missing, or is there some limitation in PDO that I should be aware of? I m happy to get rid of PDO in favour of mysqli if it can not support this, so if that s my only option, how would I perform this using mysqli instead?