如何将大表导出为 50 个较小的 csv 文件,每个文件包含 100,000 条记录

发布于 2024-09-01 13:22:31 字数 986 浏览 2 评论 0原文

我正在尝试将一个非常大的表(例如包含 5,000,000 条记录)中的一个字段导出到 csv 列表中,但不是全部导出,而是将 100,000 条记录导出到创建的每个 .csv 文件中,而不重复。请问我该怎么做?

我尝试

SELECT field_name
  FROM table_name
 WHERE certain_conditions_are_met
  INTO OUTFILE /tmp/name_of_export_file_for_first_100000_records.csv
        LINES TERMINATED BY '\n'
 LIMIT 0 , 100000

给出前 100000 条记录,但我没有采取任何措施将其他 4,900,000 条记录导出到其他 49 个文件中 - 我如何指定其他 49 个文件名?

例如,我尝试了以下操作,但 SQL 语法是错误的:

SELECT field_name
  FROM table_name
 WHERE certain_conditions_are_met
  INTO OUTFILE /home/user/Eddie/name_of_export_file_for_first_100000_records.csv
        LINES TERMINATED BY '\n'
 LIMIT 0 , 100000
  INTO OUTFILE /home/user/Eddie/name_of_export_file_for_second_100000_records.csv
        LINES TERMINATED BY '\n'
 LIMIT 100001 , 200000

并且没有创建第二个文件...

请问我做错了什么,有更好的方法吗?是否应该将 LIMIT 0 , 100000 放在第一个 INTO OUTFILE 语句之前,然后对第二个 100,000 条记录重复 SELECT 中的整个命令,等等?

感谢您的任何帮助。

艾迪

I am trying to export one field from a very large table - containing 5,000,000 records, for example - into a csv list - but not all together, rather, 100,000 records into each .csv file created - without duplication. How can I do this, please?

I tried

SELECT field_name
  FROM table_name
 WHERE certain_conditions_are_met
  INTO OUTFILE /tmp/name_of_export_file_for_first_100000_records.csv
        LINES TERMINATED BY '\n'
 LIMIT 0 , 100000

that gives the first 100000 records, but nothing I do has the other 4,900,000 records exported into 49 other files - and how do I specify the other 49 filenames?

for example, I tried the following, but the SQL syntax is wrong:

SELECT field_name
  FROM table_name
 WHERE certain_conditions_are_met
  INTO OUTFILE /home/user/Eddie/name_of_export_file_for_first_100000_records.csv
        LINES TERMINATED BY '\n'
 LIMIT 0 , 100000
  INTO OUTFILE /home/user/Eddie/name_of_export_file_for_second_100000_records.csv
        LINES TERMINATED BY '\n'
 LIMIT 100001 , 200000

and that did not create the second file...

what am I doing wrong, please, and is there a better way to do this? Should the LIMIT 0 , 100000 be put Before the first INTO OUTFILE statement, and then repeat the entire command from SELECT for the second 100,000 records, etc?

Thanks for any help.

Eddie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心欲静而疯不止 2024-09-08 13:22:31

如果您在类 UNIX 操作系统上运行,为什么不直接选择全部并通过管道传输输出:

split --lines=100000

作为概念证明:

echo '1
2
3
4
5
6
7
8
9' | split --lines=3

创建三个文件 xaaxab和包含行 1,2,34,5,67,8,9xac分别。

或者,即使在其他操作系统上,您也可以获得 GNU 工具,例如 GnuWin32,其中 split 位于 coreutils 中。

If you're running on a UNIX-like OS, why not just select the whole lot and pipe the output through:

split --lines=100000

As proof of concept:

echo '1
2
3
4
5
6
7
8
9' | split --lines=3

creates three files xaa, xab and xac containing the lines 1,2,3, 4,5,6 and 7,8,9 respectively.

Or, even on other operating systems, you can get the GNU tools, like GnuWin32, where split is in coreutils.

北城挽邺 2024-09-08 13:22:31

您可以使用循环和子查询来生成文件。以下过程可以告诉您如何做到这一点(可能有语法错误):

CREATE PROCEDURE exportSplitter(partsCount)
BEGIN
  SET rowCount = select count(*) from table;
  SET pageRowCount = rowCount / partsCount;
  SET p1 = 0;
  label1: LOOP
    SET p1 = p1 + 1;
    SELECT field_name
      FROM (SELECT * from table_name WHERE certain_conditions_are_met order by id LIMIT p1*pageRowCount) order by id desc LIMIT pageRowCount
        INTO OUTFILE /home/user/Eddie/p1
            LINES TERMINATED BY '\n'
    IF p1 < partCount THEN ITERATE label1; END IF;
    LEAVE label1;
  END LOOP label1;
END

You can use loop and sub-query to generate the files. following procedure can give you clue how to do that(it may have syntax error):

CREATE PROCEDURE exportSplitter(partsCount)
BEGIN
  SET rowCount = select count(*) from table;
  SET pageRowCount = rowCount / partsCount;
  SET p1 = 0;
  label1: LOOP
    SET p1 = p1 + 1;
    SELECT field_name
      FROM (SELECT * from table_name WHERE certain_conditions_are_met order by id LIMIT p1*pageRowCount) order by id desc LIMIT pageRowCount
        INTO OUTFILE /home/user/Eddie/p1
            LINES TERMINATED BY '\n'
    IF p1 < partCount THEN ITERATE label1; END IF;
    LEAVE label1;
  END LOOP label1;
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文