【发布时间】:2013-07-21 12:39:10
【问题描述】:
我有这些表:
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 表由包含大约 130,000 行的 CSV 填充。此 CSV 包含每个人和一些其他人员数据的唯一 varchar(40)。 CSV 中没有 ID。
对于每场马拉松,我都会得到一个 CSV,其中包含 1k - 30k 人的列表。 CSV 基本上只包含person_key 值的列表,这些值显示了哪些人参加了特定的马拉松比赛。
将数据导入person_marathon表以保持FK关系最好的方法是什么?
这些是我目前能想到的想法:
将
person_id + person_key信息从MySQL中取出,并在PHP中合并person_marathon数据,得到person_id,然后插入person_marathon表中使用临时表进行插入...但这是为了工作,我被要求永远不要在此特定数据库中使用临时表
根本不要使用
person_id,只使用person_key字段,但我必须加入varchar(40),这通常不是一件好事-
或者,对于插入,让它看起来像这样(我必须插入
<hr>否则它不会将整个插入格式化为代码):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该插入的问题在于,要在 PHP 中构建它,
imported_marathon_person_list会很大,因为它很容易成为 30,000 个select union项。不过,我不知道该怎么做。
【问题讨论】:
-
您是否为此研究过 ETL 流程? Pentaho PDI 可能是?
-
我希望能够用 PHP 编写一些东西。我只是不确定构造插入的最佳方法。我想我倾向于拉出
person_id + person_key,然后在 MySQL 插入之前将其合并到 PHP 中。这是一个非常小的项目,我不确定我们是否需要一个新工具来处理它。 -
你应该试试 Pentaho。我已经用语言(php、java)做了很多大型导入(几个 GB),并且我尝试过 Pentaho。只需尝试 30 分钟,然后告诉我们。 ;)
-
我会选择选项 2 或 3。一个 40 字节的字符串,如果索引正确,对于连接不会 可怕 - 特别是如果您的大多数查询是无论如何都要对该字符串执行查找。加载到临时文件中是一个不错的选择:到底为什么你被要求永远不要使用它们(它们不会在你的数据库连接结束后持续存在,所以谁在乎呢?)
-
@eggyal 所以临时表?如果这是最好的选择,我可以为此辩护。我只是不确定是否还有其他事情。 Pentaho 甚至可以自动化吗?临时表肯定比新工具更容易争论。
标签: php mysql insert large-data