note 128860 added to mysqli-result.fetch-all
| From: | arnelap at gmail dot com | Date: | Wed, 06 Sep 2023 02:24:54 +0000 |
| Subject: | note 128860 added to mysqli-result.fetch-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220821@lists.php.net to get a copy of this message | ||
I ran some speed tests on 10.000 records and found a significant difference in memory usage on PHP
8.1 with LiteSpeed server
fetch_all (in my case) resulted in 7mb memory used and 30ms execution time
<?php
$time_start = microtime(true);
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$query = "SELECT * FROM speedtest";
$result = $mysqli->query($query);
$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
printf("%s (%s) (%s)\n", $row["var1"], $row["var2"],
$row["var3"]);
}
echo "Peak memory usage: ".round(memory_get_peak_usage()/1048576,2)."mb | ";
echo "Memory usage: ".round(memory_get_usage()/1048576,2)."mb | ";
echo "Memory true usage: ".round(memory_get_usage(true)/1048576,2)."mb | ";
$time_end = microtime(true);
echo " | Execution time: ".(($time_end - $time_start) * 1000)."ms";
?>
Getting the same dataset with fetch_assoc resulted in only 2mb peak memory usage, and the execution
time was slightly lower as well at 27ms.
<?php
$time_start = microtime(true);
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$query = "SELECT * FROM speedtest";
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
printf("%s (%s) (%s)\n", $row["var1"], $row["var2"],
$row["var3"]);
}
echo "Peak memory usage: ".round(memory_get_peak_usage()/1048576,2)."mb | ";
echo "Memory usage: ".round(memory_get_usage()/1048576,2)."mb | ";
echo "Memory true usage: ".round(memory_get_usage(true)/1048576,2)."mb | ";
$time_end = microtime(true);
echo " | Execution time: ".(($time_end - $time_start) * 1000)."ms";
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 213.248.108.238)
----
Manual Page -- https://php.net/manual/en/mysqli-result.fetch-all.php
Edit -- https://main.php.net/note/edit/128860
Del: integrated -- https://main.php.net/note/delete/128860/integrated
Del: useless -- https://main.php.net/note/delete/128860/useless
Del: bad code -- https://main.php.net/note/delete/128860/bad+code
Del: spam -- https://main.php.net/note/delete/128860/spam
Del: non-english -- https://main.php.net/note/delete/128860/non-english
Del: in docs -- https://main.php.net/note/delete/128860/in+docs
Del: other reasons-- https://main.php.net/note/delete/128860
Reject -- https://main.php.net/note/reject/128860
Search -- https://main.php.net/manage/user-notes.php