“如何进口”;“很多”;使用PHP和外键将数据传输到MySQL?
我有这些桌子:“如何进口”;“很多”;使用PHP和外键将数据传输到MySQL?,php,mysql,insert,large-data,Php,Mysql,Insert,Large Data,我有这些桌子: create table person ( person_id int unsigned auto_increment, person_key varchar(40) not null, primary key (person_id), constraint uc_person_key unique (person_key) ) -- person_key is a varchar(40) that identifies an indivi
create table person (
person_id int unsigned auto_increment,
person_key varchar(40) not null,
primary key (person_id),
constraint uc_person_key unique (person_key)
)
-- person_key is a varchar(40) that identifies an individual, unique
-- person in the initial data that is imported from a CSV file to this table
create table marathon (
marathon_id int unsigned auto_increment,
marathon_name varchar(60) not null,
primary key (marathon_id)
)
create table person_marathon (
person_marathon _id int unsigned auto_increment,
person_id int unsigned,
marathon_id int unsigned,
primary key (person_marathon_id),
foreign key person_id references person (person_id),
foreign key marathon_id references person (marathon_id),
constraint uc_marathon_person unique (person_id, marathon_id)
)
Person表由包含约130000行的CSV填充。此CSV包含每个人和其他一些人数据的唯一varchar(40)。CSV中没有ID
对于每一场马拉松,我都会得到一份CSV,其中包含1-3万人的名单。CSV基本上只包含一个person\u key
值列表,显示哪些人参加了特定的马拉松比赛
将数据导入人马拉松
表以维护FK关系的最佳方式是什么?
以下是我目前能想到的想法:
- 将
信息从MySQL中拉出,并在PHP中合并person\u id+person\u键
数据,以在插入person\u marathon
表之前获得person\u marathon
person\u id
- 使用临时表插入。。。但这是为了工作,我被要求永远不要在这个特定的数据库中使用临时表
- 不要使用
,只需使用person\u id
字段,但我必须加入person\u key
,这通常不是一件好事varchar(40)
- 或者,对于insert,让它看起来像这样(我必须插入
,否则它不会将整个insert格式化为代码): 这个插入的问题是,要在PHP中构建它,导入的人员列表将是巨大的,因为它很容易是30000个
项。不过,我不知道还能怎么做selectunion
- 将大量行从CSV导入mysql,然后
- 进行转换,使person marathon关联通过person_id和marathon_id工作,而不是(笨拙且不受欢迎的)varchar personkey列
- 我发现在mysql环境中执行数据转换比在mysql环境之外更容易。因此,将数据输入mysql,以一种即使不是最优的方式也能保留个人马拉松关联的形式,并担心以后会改变关联方法
- 你提到临时工作台,但我认为你不需要。在persons_marathons表上设置一个临时列“personkey”。导入所有关联时,暂时将person_id留空,只需导入personkey即可。重要的是,确保personkey是associations表和persons表上的索引列。然后,您可以稍后再检查并为每个personkey填写正确的person\u id,而不用担心mysql效率低下
- 我不清楚马拉松表格数据的性质。你有成千上万的马拉松比赛要参加吗?如果是这样,我并不羡慕你在马拉松比赛中处理1份电子表格的工作。但是如果少一点,那么你也许可以用手摆好马拉松的桌子。让mysql为您生成马拉松ID。然后,在为每个马拉松导入person_marathon CSV时,请确保在与该马拉松相关的每个关联中指定该马拉松ID
此时,您的associations表应该为每一行填写person_id。现在可以安全地删除personkey列了,瞧,您有了高效的外键。您有没有为此研究过ETL过程?Pentaho PDI可能是吗?我更希望能够用PHP编写一些东西。我只是不确定构造插入的最佳方法。我想我倾向于取出
person\u id+person\u键
,然后在插入MySQL之前将其合并到PHP中。这是一个非常小的项目,我不确定我们是否需要一个新的工具来处理它。你应该试试Pentaho。我用la做了很多大的导入(几GB)
insert into person_marathon
select p.person_id, m.marathon_id
from ( select 'person_a' as p_name, 'marathon_a' as m_name union
select 'person_b' as p_name, 'marathon_a' as m_name )
as imported_marathon_person_list
join person p
on p.person_name = imported_marathon_person_list.p_name
join marathon m
on m.marathon_name = imported_marathon_person_list.m_name