Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to change data type from float to decimal of a database currently got 1 billion records

Tags:

mysql

I need to change data type from float to decimal of a database currently got 1 billion records. Is there any unforeseen issues? Thanks

like image 987
Phuong Le Avatar asked Dec 14 '15 21:12

Phuong Le


People also ask

How much data we can store in MySQL database?

The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, even if the storage engine is capable of supporting larger rows. BLOB and TEXT columns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row.


1 Answers

This is going to take a long time and take a lot of careful planning. You should plan for multiple days if you do it very carefully. If you just do an ALTER TABLE it might run for a week and then crash. I'm not kidding.

Can you avoid doing this? Can you create a view that shows the float column as decimal? You would be smart to try to avoid it.

If you must go forward with this, you should try adding a new column to the table rather than altering the existing one. Suppose your old column is called metric and the new one is called dmetric. Further suppose that your metric column is defined NOT NULL.

Then create the new column so it does allow NULLs.

Then put an index on the new column.

Then run this UPDATE query a half million times or so, until it processes no rows.

UPDATE table SET dmetric = metric
 WHERE dmetric IS NULL
 LIMIT 2000

That will do the conversion in reasonably sized chunks, and keep transactions (if you're in the innodb world) from getting too large.

Do try this on a copy of the table.

like image 73
O. Jones Avatar answered Sep 20 '22 00:09

O. Jones