If I have a table A with 1000 rows, and a structurally identical table B but this time with 1000000 rows, will an ALTER TABLE (for example to add two new columns) take longer for table B? If so will it take proportionally longer depending on how many more rows there are?
Further to this, what if the ALTER TABLE statement doesn’t just add new columns, what if it changes the structure of existing columns?
This basically comes down to how MySQL stores data at a low level which is not something I am familiar with.
2
Answers
Yes it takes longer:
As it needs to check every row, if the altered column accept the content of the row, so a bigger table in number of rows, will take longer to process.
a look into the manual will give you more detail about Alter
Appending columns can be instant when appended, or in 10.4+ if
alter_algorithm='INSTANT'
it can be added anywhere, with a different from normal storage (with storage consequences).Other operations, some are possible, documentation is extensive. You can test with:
And if it cannot be done instantly it will error.
ref: MariaDB KB InnoDB online