0

I want to add a new column to an existing table (innodb & mysql). That column should will contain UUID(). The table currently has 2 million rows.

Adding the column takes 24 seconds on my local i7, and UPDATE tbl SET uuid = UUID(); takes around 3:30 minues. That's a lot. If I encapsulate it in a transaction, I get half the time, but still a lot.

I don't really want to execute such a long query in production.

What options do I have to add UUID's for my existing 2 million rows that would take less time?

Thanks!

1
  • Why do you want to add a uuid to each row? If you already have a primary key then add a new table with a 1:1 relation. If you only need to provide a unique key, use auto increment.
    – symcbean
    Oct 2, 2015 at 21:14

3 Answers 3

1

I'm not sure that updating 2 million rows with a function that returns a complicated random id in less than 2 minutes is slow.

If you are worried that the given table will not be usable during this period, then why don't you create a 2nd table where you add the extra column, populate the other fields from the original table along with filling the uuid column up? After the new table is filled up, drop the original table and rename the new one.

1
  • Percona (Perl) and Facebook (PHP) have both released tools which do exactly that without any data loss from updates/inserts while it runs. But you can't do the same for the update.
    – symcbean
    Oct 2, 2015 at 21:14
1

Try to increase innodb_buffer_pool_sizeoption in mysql.ini(mysql.cnf)
(Good choice for innodb_buffer_pool_size option is 70-80% of system(or vmbox) memory)
Also you can temporarily set such options:

innodb_flush_log_at_trx_commit = 0
innodb_flush_method = O_DIRECT
0

While it probably won't take less time, possibly much more, the solution below could monopolize the table much less:

Run the following query on a timer of some sort, in code or a MySQL Event, until there is nothing left to update.

UPDATE tbl SET uuid = UUID() WHERE uuid IS NULL LIMIT 1000;

(Obviously, 1000 is tweakable.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.