首先,您可以考虑将查询更改为如下所示:
SELECT ticketid, fullname, creator, contents FROM swticketposts WHERE customer = 'something'
通过过滤掉您的报告不关心的客户信息,您的查询将返回更少的数据。它可以节省查询的执行时间。
其次,您可能希望使用
SUBSTRING(contents, 1, 1000) AS contents
代替contents
在你的查询中。这将使您返还部分contents
列(前千个字符);对于您想要做的事情来说,它可能(或可能不够)足够好。
Third, mysqli_
一般使用缓冲查询。这意味着当您运行查询时,整个结果集都会被放入程序的内存中。这可能就是你记忆力衰退的原因。读这个。http://php.net/manual/en/mysqlinfo.concepts.buffering.php http://php.net/manual/en/mysqlinfo.concepts.buffering.php
您可以安排逐行处理查询,执行以下操作:
$unbufResult = mysqli_query($link, $sql, MYSQLI_USE_RESULT) or trigger_error($link->error);
if ($unbufResult ) {
while ($row = $unbufResult ->fetch_assoc()) {
/* deal with the data from one row */
}
}
$unbufResult->close();
你需要小心使用close()
当您完成这些无缓冲的结果集时,因为它们会使用您的 MySQL 服务器上的资源,直到您完成为止。这可能会防止你的内存井喷。 (使用时没有什么特别的fetch_assoc()
这里;您可以使用任何您想要的方法获取每一行。)
第四,php的内存和时间限制通常针对交互式网站操作进行调整。有时报告生成需要很长时间。放打电话给set_time_limit() http://php.net/manual/en/function.set-time-limit.php在你的循环中和周围,类似这样。
set_time_limit (300); /* five minutes */
$unbufResult = mysqli_query($link, $sql, MYSQLI_USE_RESULT) or trigger_error($link->error);
if ($unbufResult ) {
while ($row = $unbufResult ->fetch_assoc()) {
set_time_limit (30); /* reset time limit on each row */
/* deal with the data from one row */
}
}
$unbufResult->close();
set_time_limit (300); /* set time limit back to five minutes when done reading */