简体   繁体   English

如何从PHP服务器生成和导出大CSV文件?

[英]How to generate and export a big CSV file from my PHP server?

My server can be PHP or nodeJS, prefer staying in PHP but if it can't be done/better in node i'll appreciate the answer. 我的服务器可以是PHP或nodeJS,更喜欢留在PHP中,但是如果不能在节点中完成/更好,我将感谢您的回答。

I want to generate and export a very big CSV file, the problem is that I can't load all the data from MySQL once, so currently I limit my data to some amount which doesn't crash the app, but it's slow and most data won't be exported. 我想生成并导出一个非常大的CSV文件,问题是我无法一次从MySQL加载所有数据,因此目前我将数据限制在一定数量内,这不会使应用程序崩溃,但速度很慢,而且大多数数据将不会导出。

I thought of this solutions to generate and export the CSV file: 我想到了生成和导出CSV文件的解决方案:

  1. Send 1..N calls to my server, each call will generate Part1...PartN CSV and the browser will append them [won't the browser crash?] 将1..N个呼叫发送到我的服务器,每个呼叫将生成Part1 ... PartN CSV,然后浏览器将它们追加[浏览器不会崩溃?]
  2. In one request, stream the data to the browser, but then how does the browser start downloading the file? 在一个请求中,将数据流式传输到浏览器,但是浏览器如何开始下载文件?

I don't mind the client will wait a lot, just want a good way to export ~200MB csv from the data I got in MySQL. 我不介意客户端会等待很多时间,只是想要一个好方法从我在MySQL中获得的数据中导出〜200MB的csv。

Thanks. 谢谢。

This is a nice solution using Mysql notation: 这是使用Mysql表示法的不错的解决方案:

SELECT *
INTO OUTFILE '/tmp/dbexport.csv' 
FIELDS ENCLOSED BY '"' 
TERMINATED BY ';' 
ESCAPED BY '"' 
LINES TERMINATED BY '\r\n'
FROM tables;

Have a look at this answer too. 也看看这个答案。

You could consider also use a .tsb (tab separated file) it does make no difference for you: 您也可以考虑使用.tsb(制表符分隔的文件),它对您没有影响:

mysql your_database -e "select * from tables" -B > export.tsv 

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

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