PHP-从MySQL获取数据的令人困惑的性能问题 [英] PHP - Confusing Performance Issue getting data from MySQL
问题描述
使用PHP/MySQLi,我想将一些数据提取到数组中.数据大小为几兆字节:
获取此数据的表转储的代码如下:
error_reporting(E_ALL);
$q = "SELECT * FROM mytable";
if (!$result = $mysqli->query($q)) {
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $query . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
return $ret;
}
if ($result->num_rows === 0) {
$ret = 0;
return $ret;
}
$ret = array();
while($row = $result->fetch_array()){
array_push($ret, $row);
}
echo mb_strlen(serialize((array)$ret), '8bit');
执行以下代码时,我得到了:
但是它说试图分配28672个字节,这远远超出了限制.为什么会这样?
以这种方式思考.
您有100美元.您去糖果店,决定一次买200个$ 1糖果棒(您的while
循环).
前100次购买将要进行,但是下次您尝试购买1美元的直板糖果时,会被告知您尝试花费1美元,但您已经花费了100美元". /p>
PHP正在报告您的 last 指令尝试使用的内存量.在这种情况下,可能是array_push($ret, $row);
调用.
Using PHP/MySQLi I wanted to extract some data to an array. The data is a few megabytes in size:
The code to get the table dump of this data follows:
error_reporting(E_ALL);
$q = "SELECT * FROM mytable";
if (!$result = $mysqli->query($q)) {
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $query . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
return $ret;
}
if ($result->num_rows === 0) {
$ret = 0;
return $ret;
}
$ret = array();
while($row = $result->fetch_array()){
array_push($ret, $row);
}
echo mb_strlen(serialize((array)$ret), '8bit');
When executing the following code I got:
But it said tried to allocate 28672 bytes which is nowhere near the limit. Why is this?
Think of it this way.
You've got $100. You go to the candy store, and decide to buy 200 $1 candy bars, one-by-one (your while
loop).
The first 100 purchases are going to go through, but the next time you try buying a $1 candy bar, you're going to get told "you tried to spend a dollar, but you've already spent $100".
PHP is reporting the amount of memory your last instruction attempted to use. In this case, it's probably the array_push($ret, $row);
call.
这篇关于PHP-从MySQL获取数据的令人困惑的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!