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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀