gpt4 book ai didi

mysql批量上传-使用另一个表中生成的主键值

转载 作者:行者123 更新时间:2023-11-29 15:49:40 25 4
gpt4 key购买 nike

我正在尝试从文本文件批量上传到 MySQL 数据库。首先,我将文件中的所有数据加载到临时辅助表(temp_rules_upload)中,批量上传后我将删除该表。然后,我希望将临时表中的每条记录插入到几个选定列的 2 个单独的表(table_a 和 table_b)中。

问题:-

  • 它首先插入 table_a 中的所有数据,然后插入 table_b 中的所有数据。
  • 如何控制在将每条记录插入table_a后,我想插入table_b,因为我需要在table_b中为每条记录使用table_a的主键。
  • 对于每条记录,我还需要从另一个表中找到starting_material_code 的主键并将其设置在table_b 中。
  • 不确定 LOAD DATA 是否适合此要求。我有数百万数据来加载其他选项(例如 JPA)非常耗时

我尝试了下面的脚本。因为它是按顺序发生的,所以我只得到 LAST_INSERT_ID() 的最后一个主键

SQL 脚本:-

LOAD DATA LOCAL INFILE 'rule_1.txt' 
INTO TABLE temp_rules_upload
COLUMNS TERMINATED BY '|'
LINES TERMINATED BY '\n' IGNORE 1 LINES
(groupname,starting_material_code,lower_limit,higher_limit,description,severity,active,created_date,created_by);

insert into table_a(active,create_date,code,description,severity)
select true,CURDATE(),groupname,description,severity from temp_rules_upload;

SET @ERROR_ID=LAST_INSERT_ID();

insert into table_b(active,create_date,code,description,groupname,higher_limit,lower_limit,compatibility_error_id,starting_material_id)
select true,CURDATE(),starting_material_code,description,groupname,higher_limit,lower_limit,@ERROR_ID,null from temp_rules_upload;

文件数据:(rule_1.txt)

1004 : 1964|1004|||not compatible with zzz|Error|Yes|6/20/2019|AAA
1004 : 1964|1964|||not compatible with ffff|Error|Yes|6/20/2019|AAA

最佳答案

我通过分成两个单独的任务来处理这个问题。我认为不可能使用 LOAD DATA LOCAL INFILE

完成所有操作

1。加载到临时表

  • 将文本文件中的数据上传到临时表中,表结构与文件相同。速度非常快,不到 10 分钟就加载完 5+00 万条数据。

2。使用存储过程将数据从临时加载到实际表中。

  • 然后使用存储过程借助游标迭代所有 5 多万条记录,并将其插入到我的 2 个表 A 和 B 中。但是,由于这涉及存储过程中的一些逻辑,因此需要 10 个小时才能上传所有记录5+000000 条数据。

我的代码:https://codereview.stackexchange.com/questions/223424/stored-procs-fine-tuning-for-processing-bulk-data-5-million-records

关于mysql批量上传-使用另一个表中生成的主键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56835767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com