Php 从巨大的表数据创建HTML文件
我的最终目标是为我正在进行的一个计费项目创建发票作为PDF文件-创建PDF是我已经做过的事情-我首先为每个发票创建一个HTML文件,然后在一个单独的过程中批量处理HTML到PDF文件 然后我的问题是我试图进入HTML文件的数据非常大。。。它存储在MySQL中(Im使用Symfony 1.4和Doctrine)。php.ini当前设置为允许php使用500米内存。我需要在同一个HTML/PDF文件中包含单个发票的所有发票行 我目前正在循环一个发票表(每个发票1行),然后从另一个表(每个发票多行)中获取发票的实际行数-我遇到的问题是内存使用量太大-有人能推荐一种替代方法吗Php 从巨大的表数据创建HTML文件,php,mysql,symfony1,doctrine,Php,Mysql,Symfony1,Doctrine,我的最终目标是为我正在进行的一个计费项目创建发票作为PDF文件-创建PDF是我已经做过的事情-我首先为每个发票创建一个HTML文件,然后在一个单独的过程中批量处理HTML到PDF文件 然后我的问题是我试图进入HTML文件的数据非常大。。。它存储在MySQL中(Im使用Symfony 1.4和Doctrine)。php.ini当前设置为允许php使用500米内存。我需要在同一个HTML/PDF文件中包含单个发票的所有发票行 我目前正在循环一个发票表(每个发票1行),然后从另一个表(每个发票多行)中
foreach ($invoices as $inv)
{
$outfile = '/ivoice' . $inv[0] . '.html';
$tempinvfile = fopen($outfile, 'w'); //open file and replace current it exists
echo '1 - Used memory : ' . number_format(memory_get_usage() - $mem) . PHP_EOL;
$invoiceLines = Doctrine::getTable('InvoiceLine')
->createQuery()
->where('invoiceid = ?', $inv[0])
->setHydrationMode(Doctrine::HYDRATE_NONE)
->execute();
echo "Count = " . count($invoiceLines) . PHP_EOL;
echo '2 - Used memory : ' . number_format(memory_get_usage() - $mem) . PHP_EOL;
$bodyhtml = '';
foreach ($invoiceLines as $invline)
{
// $bodyhtml .= '<tr>';
// $bodyhtml .= ' <td><b>' . $invline[2] . '</b></td>';
// $bodyhtml .= ' <td>£' . $invline[4] . '</td>';
// $bodyhtml .= ' <td>£' . $invline[5]. '</td>';
// $bodyhtml .= ' <td>£' . $invline[8] . '</td>';
// $bodyhtml .= ' <td>£' . $invline[9] . '</td>';
// $bodyhtml .= ' <td>£' . $invline[10] . '</td>';
// $bodyhtml .= ' <td>£' . $invline[12] . '</td>';
// $bodyhtml .= '</tr>';
}
echo '3 - Used memory : ' . number_format(memory_get_usage() - $mem) . PHP_EOL;
// fwrite($tempinvfile, $bodyhtml);
// fclose($tempinvfile);
}
echo 'Used memory : ' . number_format(memory_get_usage() - $mem) . PHP_EOL;
echo "Done processing, time = " . (time() - $start) . PHP_EOL;
因此,我可以清楚地看到,主内存的使用是在从表中抓取发票行时进行的。但我不知道最好的方法。。。。。如何在每个循环结束时回收内存
我曾经尝试过使用DoctrinePager并一次循环一页,但使用了比上述方法更多的内存
欢迎提出建议
编辑:
所有文件操作都被注释掉并在echo 3之后添加了unset($invoiceLines)
的内存输出:
Starting Processing
1 - Used memory : 615,712
Count = 39
2 - Used memory : 1,033,248
3 - Used memory : 1,033,328
after end loop - Used memory : 1,033,328
1 - Used memory : 1,055,432
Count = 11
2 - Used memory : 1,118,176
3 - Used memory : 1,118,176
after end loop - Used memory : 1,118,176
1 - Used memory : 1,140,280
Count = 30340
2 - Used memory : 89,061,760
3 - Used memory : 88,977,680
after end loop - Used memory : 88,977,680
1 - Used memory : 88,999,784
Count = 156
2 - Used memory : 89,482,968
3 - Used memory : 89,482,968
after end loop - Used memory : 89,482,968
1 - Used memory : 89,505,584
Count = 3867
2 - Used memory : 100,737,464
3 - Used memory : 100,737,464
after end loop - Used memory : 100,737,464
Used memory : 100,737,464
Done processing, time = 0
编辑2:Suggestion by@Crack->added profiler:false到databases.yml文件。。以下是内存结果:
Starting Processing
1 - Used memory : 600,392
Count = 39
2 - Used memory : 1,006,768
3 - Used memory : 1,006,848
after end loop - Used memory : 896,352
1 - Used memory : 903,192
Count = 11
2 - Used memory : 954,624
3 - Used memory : 951,824
after end loop - Used memory : 922,576
1 - Used memory : 929,416
Count = 30340
2 - Used memory : 88,840,104
3 - Used memory : 88,751,672
after end loop - Used memory : 863,168
1 - Used memory : 870,008
Count = 156
2 - Used memory : 1,342,816
3 - Used memory : 1,340,016
after end loop - Used memory : 889,392
1 - Used memory : 896,232
Count = 3867
2 - Used memory : 12,117,080
3 - Used memory : 12,114,280
after end loop - Used memory : 915,616
Used memory : 915,616
Done processing, time = 0
读取1000行后,使用fwrite()
,然后重置$bodyhtml($bodyhtml='';
)。这样就不会在内存中存储大字符串
编辑
哦,条令必须将所有以前读取的行存储在
$invoiceLines
中的某个缓存中。。。尝试寻找解决方案。在第三次回显后,您是否可以尝试取消设置($invoiceLines)和fclose($tempinvfile),并向我们展示差异?@corretge-注释掉文件操作行并在上面添加unset-结果…导致内存消耗的不是$bodyhtml-而是查询结果-您能告诉我如何一次只循环1000行吗。。。我尝试过使用DoctrinePager,但这会给出与问题中类似的内存使用情况。正确的解决方案是看看您是否可以对DoctrinePager的内存使用做些什么。如果没有,您可以对查询应用LIMIT
,并在几次过程中生成HTML。我只是想使用LIMIT/Offset-然后循环,直到HTML文件中有完整的行集合-但问题更多的是内存没有恢复-我要创建4000张发票…也许这会有帮助-?HOLY S&*T。。。谢谢你-剖析者:false带来了巨大的不同-在问题中发布了结果。。。。
Starting Processing
1 - Used memory : 600,392
Count = 39
2 - Used memory : 1,006,768
3 - Used memory : 1,006,848
after end loop - Used memory : 896,352
1 - Used memory : 903,192
Count = 11
2 - Used memory : 954,624
3 - Used memory : 951,824
after end loop - Used memory : 922,576
1 - Used memory : 929,416
Count = 30340
2 - Used memory : 88,840,104
3 - Used memory : 88,751,672
after end loop - Used memory : 863,168
1 - Used memory : 870,008
Count = 156
2 - Used memory : 1,342,816
3 - Used memory : 1,340,016
after end loop - Used memory : 889,392
1 - Used memory : 896,232
Count = 3867
2 - Used memory : 12,117,080
3 - Used memory : 12,114,280
after end loop - Used memory : 915,616
Used memory : 915,616
Done processing, time = 0