I have a table that has two string columns that repeat a lot, and in the next stage, some counts and conditions need to be done, lets say they are called company_name
and person_name
.
The table has 200k rows.
I am creating a table for companies
and persons
, each has id, name
.
I also added company_id_fk
and person_id_fk
and set FK constraints with nullable default null on the source table.
I have populated the new tables with distinct values from the source table.
Now I am trying to populate the FKs by matching the strings (I couldn't think of any better way!), which looks like this:
UPDATE data_table, companies, persons
SET data_table.person_id_fk = persons.id, data_table.company_id_fk = companies.id
WHERE
data_table.company_name = companies.`name`
AND data_table.person_name = person.`name`;
This has now been running for 1500 seconds and is still not done.
Am I doing this correctly?
What are the more effective and correct ways to normalize data_table
and add FKs?
Thanks.