ALTER TABLE without locking the table?

后端 未结 19 1849
梦毁少年i
梦毁少年i 2020-11-30 17:41

When doing an ALTER TABLE statement in MySQL, the whole table is read-locked (allowing concurrent reads, but prohibiting concurrent writes) for the duration of the statement

相关标签:
19条回答
  • 2020-11-30 17:58

    If you cannot afford downtime for your database when doing application updates you should consider maintaining a two-node cluster for high availability. With a simple replication setup, you could do almost fully online structural changes like the one you suggest:

    • wait for all changes to be replicated on a passive slave
    • change the passive slave to be the active master
    • do the structural changes to the old master
    • replicate changes back from the new master to the old master
    • do the master swapping again and the new app deployment simultaneously

    It is not always easy but it works, usually with 0 downtime! The second node does not have to be only passive one, it can be used for testing, doing statistics or as a fallback node. If you do not have infrastructure replication can be set up within a single machine (with two instances of MySQL).

    0 讨论(0)
  • 2020-11-30 17:59

    I recommend Postgres if that's an option. With postgres there is essentially no downtime with the following procedures:

    • ALTER TABLE ADD COLUMN (if the column can be NULL)
    • ALTER TABLE DROP COLUMN
    • CREATE INDEX (must use CREATE INDEX CONCURRENTLY)
    • DROP INDEX

    Other great feature is that most DDL statements are transactional, so you could do an entire migration within a SQL transaction, and if something goes wrong, the entire thing gets rolled back.

    I wrote this a little bit ago, perhaps it can shed some more insight on the other merits.

    0 讨论(0)
  • 2020-11-30 18:02

    Using the Innodb plugin, ALTER TABLE statements which only add or drop secondary indexes can be done "quickly", i.e. without rebuilding the table.

    Generally speaking however, in MySQL, any ALTER TABLE involves rebuilding the entire table which can take a very long time (i.e. if the table has a useful amount of data in it).

    You really need to design your application so that ALTER TABLE statements do not need to be done regularly; you certainly don't want any ALTER TABLE done during normal running of the application unless you're prepared to wait or you're altering tiny tables.

    0 讨论(0)
  • 2020-11-30 18:03

    If anyone is still reading this or happens to come here, this is the big benefit of using a NoSQL database system like mongodb. I had the same issue dealing with altering the table to either add columns for additional features or indexes on a large table with millions of rows and high writes. It would end up locking for a very long time so doing this on the LIVE database would frustrate our users. On small tables you can get away with it.

    I hate the fact that we have to "design our tables to avoid altering them". I just don't think that works in today's website world. You can't predict how people will use your software that's why you rapidly change things based on user feedback. With mongodb, you can add "columns" at will with no downtime. You don't really even add them, you just insert data with new columns and it does it automatically.

    Worth checking out: www.mongodb.com

    0 讨论(0)
  • 2020-11-30 18:03

    Dummy columns are a good idea if you can predict their type (and make them nullable). Check how your storage engine handles nulls.

    MyISAM will lock everything if you even mention a table name in passing, on the phone, at the airport. It just does that...

    That being said, locks aren't really that big a deal; as long as you are not trying to add a default value for the new column to every row, but let it sit as null, and your storage engine is smart enough not to go writing it, you should be ok with a lock that is only held long enough to update the metadata. If you do try to write a new value, well, you are toast.

    0 讨论(0)
  • 2020-11-30 18:04

    TokuDB can add/drop columns and add indexes "hot", the table is fully available throughout the process. It is available via www.tokutek.com

    0 讨论(0)
提交回复
热议问题