I have these tables:
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 table is populated by a CSV that contains about 130,000 rows. This CSV contains a unique varchar(40) for each person and some other person data. There is no ID in the CSV.
For each marathon, I get a CSV that contains a list of 1k - 30k persons. The CSV contains essentially just a list of person_key values that show which people participated in that specific marathon.
What is the best way to import the data into the person_marathon table to maintain the FK relationship?
These are the ideas I can currently think of:
Pull the
person_id + person_keyinformation out of MySQL and merge theperson_marathondata in PHP to get theperson_idin there before inserting into theperson_marathontableUse a temporary table for insert... but this is for work and I have been asked to never use temporary tables in this specific database
Don't use a
person_idat all and just use theperson_keyfield but then I would have to join on avarchar(40)and that's usually not a good thingOr, for the insert, make it look something like this (I had to insert the
<hr>otherwise it wouldn't format the whole insert as code):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_nameThe problem with that insert is that to build it in PHP, the
imported_marathon_person_listwould be huge because it could easily be 30,000select unionitems. I'm not sure how else to do it, though.
person_id + person_keyand then merging it in PHP before the MySQL insert. This is a very small project and I'm not sure we need a new tool to handle it.