简体   繁体   English

如何使用PHP处理大量数据?

[英]how to handle large sets of data with PHP?

My web application lets user import an excel file and writes the data from the file into the mysql database. 我的Web应用程序允许用户导入excel文件,并将文件中的数据写入mysql数据库。 The problem is, when the excel file has lots of entries, even 1000 rows, i get an error saying PHP ran out of memory. 问题是,当excel文件有很多条目,甚至1000行时,我收到一条错误消息,说PHP内存不足。 This occurs while reading the file. 读取文件时会发生这种情况。 I have assigned 1024MB to PHP in the php.ini file. 我在php.ini文件中为PHP分配了1024MB。

My question is, how to go about importing such large data in PHP. 我的问题是,如何在PHP中导入如此大的数据。

I am using CodeIgniter. 我正在使用CodeIgniter。 for reading the excel file, i am using this library. 为了读取Excel文件,我正在使用这个库。


SOLVED. 解决了。 I used CSV instead of xls. 我使用CSV而不是xls。 and I could import 10,000 rows of data within seconds. 我可以在几秒钟内导入10,000行数据。 Thank you all for your help. 谢谢大家的帮助。

As others have said, 1000 records is not much. 正如其他人所说,1000条记录并不多。 Make sure you process the records one at a time, or a few at a time, and that the variables you use for each iteration go out of scope after you're finished with that row or you're reusing the variables. 确保您一次或一次处理记录,并且在完成该行或重用变量后,每次迭代使用的变量都超出范围。

If you can avoid the necessity of processing excel files by exporting them to csv, that's even greater, cause then you wouldn't need such a library (which might or might not have its own memory issues). 如果您可以通过将excel文件导出到csv来避免处理excel文件的必要,那会更大,因为那么您就不需要这样的库了(它可能有也可能没有自己的内存问题)。

Don't be afraid of increasing memory usage if you need to and that solves the problem, buying memory is the cheapest option sometimes. 如果需要,不要害怕增加内存使用量,这可以解决问题,有时购买内存是最便宜的选择。 And don't let the 1 GB scare you, it is a lot for such a simple task, but if you have the memory and that's all you need to do, then its good enough for the moment. 而且不要让1 GB吓到您,对于这样一个简单的任务来说,这是很多事情,但是如果您有足够的内存,而这正是您需要做的,那么目前它已经足够了。

And as a plus, if you are using an old version of PHP, try updating to PHP 5.4 which handles memory much better than its predecessors. 另外,如果您使用的是旧版本的PHP,请尝试更新到PHP 5.4,它比以前的版本处理内存要好得多。

Instead of inserting one a time in a loop. 而不是一次插入一个循环。 Insert 100 row at a time. 一次插入100行。

You can always run 你总是可以跑

INSERT INTO myTable (clo1, col2, col2) VALUES 
(val1, val2), (val3, val4), (val5, val6) ......

This way number of network transaction will reduce thus reducing resource usage. 这样,网络事务数将减少,从而减少资源使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM