打开输出缓冲:http://php.net/manual/en/function.ob-start.php
<?php
ob_start();
// do work...
ob_end_flush();
?>
编辑
以下 php 在 0.014 秒内获取 10K 产品并将其输出为一个没有问题的表格。
<?php
ob_start();
$conn = new Mysqli("localhost", "vldb_dbo", "pass", "vldb_db");
$startTime = microtime(true);
$result = $conn->query(sprintf("call list_products(%d)", 10000));
echo "<table border='1'>";
while($row = $result->fetch_assoc()){
echo sprintf("<tr><td>%s</td><td>%s</td></tr>", $row["prod_id"], $row["name"]);
}
echo "</table>";
echo sprintf("<br/>Page generated in %s secs",number_format(microtime(true) - $startTime, 6, ".", ""));
$result->close();
$conn->close();
ob_end_flush();
?>
SQL 脚本
drop procedure if exists list_products;
delimiter #
create procedure list_products
(
in p_prod_id int unsigned
)
begin
select * from product where prod_id between 1 and p_prod_id;
end #
delimiter ;
除了运行时潜水外,50K 记录也可以正常工作 - 页面在 0.112902 秒内生成
EDIT2
http://phplens.com/lens/php-book/optimizing-debugging-php.php
摘自上述链接:
加快上述速度的另一种方式
代码将使用输出缓冲。
这将累积输出字符串
内部,并将输出发送到一个
在剧本结尾处拍摄。这
减少网络开销
基本上以更多的成本为代价
内存和延迟增加。在
我的一些代码完全由
回声语句,性能
改进了 15%
观察到。