ocwirk
ocwirk

Reputation: 1089

Move lots of records from one table to another in mysql using java

I am trying to do some garbage collection in my database by moving records which are not required to graveyard tables. So if I have these four tables -

  1. Version (id is primary key)
  2. Version_history (version_id is foreign key)
  3. Version_graveyard (Discarded Version rows here)
  4. Version_history_graveyard (Discarded Version history rows here)

It consists of the following steps -

So as you can see this would require repeating the select query many times. I thought of getting all the versions from the first query and then using NamedParameterJdbcTemplate batchupdate function and In clause for all the subsequent queries like -

Insert into Version_Graveyard Select * from Version where id in (?, ?, ..... ?);

Delete from Version Where id in (?, ?, .. ?)

But the problem is that number of versions to be deleted could be many thousands (~20K on first run) and the according to documentation I can't have more than 1000 values inside the IN clause.

So what could be other way to avoid repeating the same select query for multiple steps.

Upvotes: 1

Views: 368

Answers (2)

Paul Wasilewski
Paul Wasilewski

Reputation: 10372

It would possible to add a special field (flag) in the Version and Version_history table which indicates that the record should be next time moved to the graveyard tables. For example exportToGraveyard.

Next step is to do a UPDATE instead of a SELECT which set the field. All other querys can now access all records which should be moved by the flag.

For performance optimzation it would be also possible to add an INDEX on the field. But beware with an INDEX the UPDATE statement should be slower.

Upvotes: 1

Jürgen
Jürgen

Reputation: 418

You could combine multiple IN clauses with OR.

 SELECT * FROM VERSION WHERE ID IN (...) OR ID IN (...) OR ID IN (...)

Upvotes: 0

Related Questions