dongmimeng5500 2018-06-13 03:40
浏览 91
已采纳

PHP / Symfony:写入CSV大文件

Thank you for helping.

My Spec:

  1. I have a very large CSV file
  2. I need to process the file and output another large CSV file

My environment:

  1. php7.0
  2. Symfony 3.4 framework

My current solution:

I am using Symfony Serializer component to read my file, I then process my file and output the file. All in memory. Operation takes around 20 minutes to complete.

I run the script from a Symfony command. I have a service to read the file and a service to export the file.

An improved solution 1:

  1. I can load the CSV file into a database table like explained here, with "LOAD DATA LOCAL INFILE" sql query. Very fast operation
  2. I can then process my data and save it to another table
  3. Then I would use "SELECT ... INTO OUTFILE 'file_name'" to output the file

Advantage: -SQL centered - No memory problem

Disavantage: -DB operations during processing might be expensive

An improved solution 2:

  1. I can read the CSV file line by line
  2. I process the line
  3. I write the line to the output file

Adv: No memory issue Disav: Could take a LOT of time.

An improved solution 3:

  1. I can load the CSV file into a database table like explained here, with "LOAD DATA LOCAL INFILE" sql query. Very fast operation
  2. I can then process my data in chunks of 1000 and write them to a file

What solution would you use? Or do you have any better solution?

  • 写回答

1条回答 默认 最新

  • ds34222 2018-06-13 04:21
    关注

    solution 2, without a database. Read from csv, process and output to csv just like someone mentioned in the comments, use fgetcsv() and fputcsv(). Going line by line should hardly consume any memory and it will remove the need for a database in between. The issue with these types of operations is the sequential nature of reading csv files as streams, eventually the speed of the process will boil down to the speed of the operation on the data in between the read and write operations. Using a database in between will just slow the entire process down and be somewhat wasteful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵