I’ve got a table with 10M rows, and I’m trying to ALTER TABLE to add another column (a VARCHAR(80)).
From a data-modelling perspective, that column should be NOT NULL – but the amount of time it takes to run the statement is a consideration, and the client code could be changed to deal with a NULL column if that’s warranted.
Should the NULL-ability of the column I’m trying to add significantly impact the amount of time it takes to add the column either way?
More Information
The context in which I’m doing this is a Django app, with a migration generated by South – adding three separate columns, and adding an index on one of the newly-added columns. Looking at the South-generated SQL, it spreads this operation (adding three columns and an index) over 15 ALTER TABLE statements – which seems like it will make this operation take a whole lot longer than it should.
I’ve seen some references that suggest that InnoDB doesn’t actually have to create a field in the on-disk file for nullable fields that are NULL, and just modifies a bitfield in the header. Would this impact the speed of the ALTER TABLE operation?
I don’t think the nullability of the column has anything to do with the speed of
ALTER TABLE. In most alter table operations, the whole table – with all the indexes – has to be copied (temporarily) and then the alteration is done on the copy. With 10M rows, it’s kind of slow. From MySQL docs:If you want to make several changes in a table’s structure, it’s usually better to do them in one
ALTER TABLEoperation.Allowing client code to make changes in tables is probably not the best idea – and you have hit on one good reason for not allowing that. Why do you need it? If you can’t do otherwise, it would probably be better – for performance reasons – to allow your client code to be creating a table (with the new column and the PK of the existing table) instead of adding a column.