Cystack
Cystack

Reputation: 3551

MySQL timestamp to datetime conversion

I have a very big MySQL table (millions of rows) that gets new data very 20 minutes. I did a mistake at first and have a timestamp data, while what I really wanted was a datetime column. How can I either eidt the current table or migrate it to a new one, as fast as possible WHILE still being able to append new data to either the old or the new table ?

Upvotes: 0

Views: 134

Answers (1)

amaidment
amaidment

Reputation: 7268

That rather depends on what/who is using the database, and what control you have...

One approach might be to:

  • create a new table (with desired data structure) (CREATE TABLE...)
  • amend code/processes that are writing to the database, so that they write to both the old and the new tables (but continue to read from the old table)
  • when everything is writing to both, copy the old data from the old table to new table (INSERT ... SELECT ...)
  • amend code/processes that are reading from the database so that they read from the new table (but continue to write to both)
  • when everything is reading from the new table, amend the code/processes to only write to the new table
  • when everything is reading from and writing to the new table (alone), you can delete the old table. (DROP TABLE...)

You could also do something similar with by altering the existing tables (i.e. creating a new column), but if your data is updating every 20 mins, you may have a very large table, in which case the ALTER TABLE query could take a while to run - so I'd recommend migrating to a new table instead.

Upvotes: 1

Related Questions