使用 MySQL LOAD DATA 命令过滤正在加载的数据

发布于 2024-10-20 05:11:14 字数 264 浏览 0 评论 0原文

我正在使用以下命令。

LOAD DATA  INFILE 'source.txt'
INTO TABLE tblData
CHARACTER SET utf8
FIELDS TERMINATED BY '\t';

这工作正常,但我得到的 source.txt 是一个 800MB 的文件,其中只有我需要的 10% 行。我无法在加载之前对文本文件进行过滤。我可以根据某一特定列的值过滤结果。有没有办法可以在加载语句中指定此条件,以便仅加载所需的行。

I am using the following command.

LOAD DATA  INFILE 'source.txt'
INTO TABLE tblData
CHARACTER SET utf8
FIELDS TERMINATED BY '\t';

This works fine, but the source.txt that I get is an 800MB file with only 10% rows that are required by me. I cannot do the filtering on the text file before load. I can filter my results based on the value of one specific column. Is there a way I can specify this condition in my load statement so only the required rows are loaded.

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

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

发布评论

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

评论(2

久夏青 2024-10-27 05:11:14

不,至少不使用LOAD DATA INFILE

但是,您可以创建一个脚本来解析您的文件并仅插入符合您条件的记录

Nope, at least not with LOAD DATA INFILE.

You can however create a script that parses your file and only inserts the records that matches your criteria

孤芳又自赏 2024-10-27 05:11:14

您可以让脚本创建一个临时文件,将 LOAD DATA INFILE 调用到临时文件中,然后将临时文件过滤到最终目标。

CREATE TEMPORARY FILE temp LIKE tblData;
LOAD DATA  INFILE 'source.txt'
    INTO TABLE temp
    CHARACTER SET utf8
    FIELDS TERMINATED BY '\t';
INSERT INTO tblData (field1, field2)
    (SELECT field1, field2 FROM temp WHERE some condition);

如果您的系统配置为确保临时文件保留在内存中,您仍然应该获得良好的性能。

You can have your script create a temporary file, call LOAD DATA INFILE into the temporary file, and then filter the temporary file into the final destination.

CREATE TEMPORARY FILE temp LIKE tblData;
LOAD DATA  INFILE 'source.txt'
    INTO TABLE temp
    CHARACTER SET utf8
    FIELDS TERMINATED BY '\t';
INSERT INTO tblData (field1, field2)
    (SELECT field1, field2 FROM temp WHERE some condition);

If your system is configured to make sure the temporary file stays in memory, you should still get good performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文