Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Update parent table id from old table that has relation

I am creating new tables in my database because im trying to normalize them.

I am having to update these new tables with my old data.

i have populated tbl_activity and tbl_class from results_class table

I have also populated tbl_activity_class with all the class id's from tbl_class.

Now i need to update tbl_activity_class with my activity_id's from tbl_activity.

results_class has the right relationships between activity and class

How can i update tbl_activity_class activity_id from tbl_activity activity_id where results_class activity and class match class_id of tbl_class.

hope i explained this well enough.
  • 5
  • 4
1 Solution
bignadadAuthor Commented:
I came up with this to get the data into a tmp table so i could easily get it into the table needed.

However its taking forever to execute. Is there a quick way to run this query?

INSERT INTO tbl_activity_copy (activity_id, activity,class_id,class)
FROM tbl_class
  INNER JOIN results_class
    ON tbl_class.class = results_class.class
  INNER JOIN tbl_activity
    ON results_class.activity = tbl_activity.activity

Open in new window

Aaron TomoskyTechnology ConsultantCommented:
looks pretty basic, do you have indexes on those columns you are joining on?

Try doing left joins for this insert, then delete rows with null you don't want after. that might go faster.
bignadadAuthor Commented:
i have index on results_class activity and class unique.

What might that look like?
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Aaron TomoskyTechnology ConsultantCommented:
Instead of inner join use left join
bignadadAuthor Commented:
I tried left and it takes longer to run select query.

With either inner or left when I add insert the query takes forever then times out.  What are my options?
I'm using dbforge
Aaron TomoskyTechnology ConsultantCommented:
are you truncating the table first?
bignadadAuthor Commented:
The tmp table I'm inserting to?  Yes. It's empty
Aaron TomoskyTechnology ConsultantCommented:
I'm out of ideas, it's a simple query, not much to optimize. I suppose it could be running out of memory or something.
bignadadAuthor Commented:
Hmm.  Im not sure what's going on. The tables are both over 100k rows.

I'll post more details and sample data when I get to pc
Tomas Helgi JohannssonCommented:

Are the columns  activity_id in tbl_activity and class_id in tbl_class auto_increment fields ?
If not you should consider that. Note the tbl_activity_class should not have auto_increment fields.

Then populating the tables would be as follows
insert into tbl_activity (activity) select distinct activity from results_class ;
insert into tbl_class (class ) select distinct class from results_class;
insert into tbl_activityclass (activity_id, class_id ) 
   select a.activity_id, c.class_id 
   from tbl_activity a, tbl_class c, results_class r
   where a.activity = r.activity
   and c.class = r.class;

Open in new window

Make sure that you have the following indexes

index on activity_id on tbl_activity
index on class_id on tbl_class
index on (activity_id, class_id ) on tbl_activity_class
index on (class_id, activity_id ) on tbl_activity_class

Those indexes will speed up selects on your tables.

      Tomas Helgi

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now